【发布时间】:2021-01-19 13:11:21
【问题描述】:
String[][] array = {{"Checkup", "60"},
{"Repairing tooth", "150"},
{"Cleaning", "30"}}; // Menu of treatments
String[] array2 = new String [10]; // New array that saves up to 10 elements(treatments)
int cost = 0;
int treatment = 0;
Scanner input = new Scanner(System.in);
System.out.println("Control" + " " + "1");
System.out.println("Repair tooth:" + " " + "2");
System.out.println("Cleaning:" + " " + "3");
int n = array.length;
for (int i=0; i<n; i++) {
for (int j=0; i<n ; j++) {
System.out.println();
treatment = input.nextInt();
if (treatment==1) {
cost += Integer.parseInt(array[i][1]);
System.out.print("Total cost so far: " + cost);
}
if (treatment==2) {
cost += Integer.parseInt(array[i+1][1]);
System.out.print("Total cost so far: " + cost);
}
if (treatment==3) {
cost += Integer.parseInt(array[i+2][1]);
System.out.print("Total cost so far: " + cost);
}
}
}
我该如何从这里继续前进?我想我必须将输入存储在新数组中并在 10 次处理后退出循环,或者为用户添加一个选项,以便在完成后打印出收据。
收据需要打印所有选择的治疗方法以及每种治疗方法的费用。我还需要添加一个变量来为所有选择的治疗添加总量。
【问题讨论】:
标签: java arrays loops for-loop if-statement