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);
}
}
答案 0 :(得分: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++)