import java.util.Scanner;
class bazar
{
void calculate()
{
int sum=0;
Scanner sc = new Scanner (System.in);
System.out.println("Hi ! welcome to out advance calculator");
System.out.println("Enter the number of items that you wish to compute");
int c = sc.nextInt();
String item[] = new String[c];
int price[] = new int[c];
sc.nextLine();
for (int i=1; i<=c; i++)
{
System.out.println("please enter the item name : ");
item[i] = sc.nextLine();
System.out.println();
System.out.println("please enter the price of " +item[i]+":");
price[i] = sc.nextInt();
sc.nextLine();
sum=sum+price[i];
}
//display part
for (int k=1; k<=c; k++)
{
System.out.println( "ITEM PRICE");
System.out.println (item[k]+" "+price[k]);
}
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println("YOUR BILL TOTAL HAS COME TO----------------->"+sum);
}
}
-4
A
回答
1
int price[] = new int[c];
sc.nextLine();
for (int i=1; i<=c; i++)
你正在創建使用C索引的數組。我們假設c是10.這意味着你的索引是0-9,總共有10個索引。然後,您循環到,包括 c。在我們的例子,這意味着你想嘗試進入指數10,但指數最高爲9
希望此相反:
for (int i=0; i<c; i++)
相關問題
- 1. 數組索引越界異常
- 2. 數組索引越界異常android
- 3. 數組索引越界的異常:2
- 4. 數組索引越界異常0
- 5. 數組索引越界異常JAVA
- 6. 歸併數組索引越界異常
- 7. Java數組索引越界異常的不管數組長度
- 8. (Java)數組越界異常
- 9. 數組越界異常:100
- 10. 越界異常乘數組
- 11. 數組越界異常
- 12. 數組越界異常?
- 13. PHPExcel表索引越界異常
- 14. 索引越界異常在C#
- 15. 字符串索引越界異常java
- 16. 奇怪的索引越界異常
- 17. 的Android - 光標索引越界異常
- 18. 索引越界異常的java
- 19. 如何創建索引越界異常?
- 20. Android遊標越界異常索引0
- 21. 字符串索引越界異常中
- 22. 數組索引越界
- 23. 數組索引越界
- 24. 爲什麼我得到一個數組索引越界異常?
- 25. 標量乘法 - 數組索引越界異常
- 26. 數組索引越界異常的JTable中
- 27. Java數組索引越界異常修復
- 28. 刷新主題()給數組索引越界異常的LWUIT
- 29. 爲什麼我得到一個數組索引越界異常?
- 30. 如何解決數組索引越界異常
數組是0指數的,而不是1索引。你的for循環應該是:'for(int i = 0; i
請不要要求我們爲你調試代碼......閱讀規則。此外,使用代碼標籤。最後,歡迎您,但您需要告訴我們您嘗試的內容以及控制檯的確切錯誤。 – Brendan