数组显示异常中的索引,但不知道在哪里


import java.util.Scanner;
public class Question1 {
public static void main(String[] args){      
System.out.println("      Welcome to Shopping");    
System.out.println("Please enter your shopping (item$price and seperate by comma): ");
Scanner scanner = new Scanner(System.in);
String list = scanner.next();
//here is my code     
System.out.println("Here is the list of items:");
int p = 0;
int count = 0;
for(int i = 0; i < list.length(); i++) {
if(list.charAt(i) == ',') 
count++;
}
int[] price = new int[count];
String[] temp  = list.split("\,");
for(int i=0;i<=count;i++){
int a = i+1;
**price[i] = Integer.parseInt(temp[i].substring(temp[i].indexOf("$")+1,temp[i].length()));**
p+=price[i];
System.out.println("No."+a+" "+temp[i].substring(0,temp[i].indexOf("$")));

}
System.out.println("The total price is: $"+p+".");
System.out.println("Thank you for using this program!!");
}

}
}

//我不知道为什么它不能打印出来,并且显示了类似array的内容从…里面

我试着打印出一个类似于物品的类型名称$price,nextwill看起来像芒果$12,番茄$14,打印出来像1.芒果2.番茄

最后,打印出其总成本26

它可以将第一个for循环的i更改为1,并且与设置i<计数

相关内容

最新更新