8 月,1 周
Questions & Answers
54
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);
}
}
# 1 楼答案
您正在使用c索引创建一个数组。假设c是10。这意味着您的索引是0-9,总共有10个索引。然后循环到并包含c。在我们的示例中,这意味着您将尝试访问索引10,但最高的索引是9
你想要这个: