它应该像这样输入
我想知道如何让这样的用户输入并存储在数组中。
Enter Order Number (0 to stop): M3487
Enter Quantity: 2
Enter Order Number (0 to stop): W3876
Enter Quantity: 3
Enter Order Number (0 to stop): R9983
Enter Quantity: 3
Enter Order Number (0 to stop): 0
当我输入代码“M3487”时,它不会输入数量,它会结束程序。
这是我到目前为止的代码。我是初学者,所以请耐心等待。
package catalog;
import java.util.*;
public class Catalog {
static String products[] = new String[3];
static int answer;
public static void main(String[] args) {
System.out.println("------------------");
System.out.println("Shopping Catalog");
System.out.println("------------------");
String[] pCode = new String[3];
float pPrice[] = new float[3];
int orderNum = 0;
int quantity=0;
Scanner s = new Scanner(System.in);
System.out.println("------------------------------------------");
System.out.println("condensed milk [M3487], $9.50 per can.");
System.out.println("");
System.out.println("Distilled Water [W3876], $3.00 a bottle.");
System.out.println("");
System.out.println("Pack Rice [R9983], $12.75 for 5lbs.");
System.out.println("------------------------------------------");
do{
System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();
orderNum++;
if(pCode[orderNum] == ("M3487")){
System.out.println("condensed milk $9.50");
System.out.println("Enter Quantity");
quantity = s.nextInt();
}//close if statement
if(answer == 0){
break;
}//close if
}while(true);//close while loop
}//close main method
}//close class
答案 0 :(得分:0)
System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();
orderNum++; //This is the problem
if(pCode[orderNum] == ("M3487"))//It will not work because you have change the index
删除
orderNum++;
因为您已在pCode[0]
零索引处插入字符串并在pCode[1]
处搜索。
将您的代码更改为:
System.out.println("Please enter order number (0 to stop)");
pCode[orderNum] = s.nextLine();
if(pCode[orderNum].equals("M3487")){
System.out.println("condensed milk $9.50");
System.out.println("Enter Quantity");
quantity = s.nextInt();
}
orderNum++;