【问题标题】:How do i store the chosen values after user input? The goal is to print out array2 as a receipt with all the chosen treatments when the user is done用户输入后如何存储选择的值?目标是在用户完成后打印出 array2 作为收据,其中包含所有选择的治疗
【发布时间】: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


    【解决方案1】:

    这就是你想要做的,因为治疗是固定的,所以你可以将它们索引为 0、1、2。你可以做的一件事是制作一个哈希图,你可以在其中存储治疗名称和每次用户想要输入时的成本 (String,int)。 看下面的代码

    import java.util.*;
    import java.util.HashMap;
    
    public class treatment {
    
    public static void main(String []args) {
        
        String[][] array = {{"Checkup", "60"},
                    {"Repairing tooth", "150"},  
                    {"Cleaning", "30"}};  // Menu of treatments
    
         // New array that saves up to 10 elements(treatments)
        HashMap<String, Integer> treat = new HashMap<String, Integer>();
    
    
        int cost = 0;  
        int treatment = 0;
    
        Scanner input = new Scanner(System.in);
    
    
    
        int n = array.length;
        int i =0;
        char c = '\0';
    
        do {
            System.out.println("\n\nControl" + " " + "1");
            System.out.println("Repair tooth:" + " " + "2");
            System.out.println("Cleaning:" + " " + "3");
            System.out.println("Exit: " + "-1");
            System.out.println();
            System.out.print("Enter treatment value (1, 2, 3): ");
            treatment = input.nextInt();
    
            if (treatment==1){ 
                i = 0;
                cost += Integer.parseInt(array[0][1]);
                System.out.println("\nTotal cost so far: " + cost);
    
            }
            else if (treatment==2) {
                i = 1;
                cost += Integer.parseInt(array[1][1]);
                System.out.println("\nTotal cost so far: " + cost);
            }
            else if (treatment==3) {
                i = 2;
                cost += Integer.parseInt(array[2][1]);
                System.out.println("\nTotal cost so far: " + cost);
            }
            treat.put(array[i][0], cost);
    
        } while (treatment != -1);
    
        System.out.println("Total COst is : " + cost);
        System.out.println("The treatements you opt for are:\n");
        System.out.println(treat);
        System.out.println("\n");
        
       }
    }
    

    【讨论】:

    • 谢谢!我不允许为此使用 HashMap,但我想我可以继续使用类似的解决方案 :)
    • 我很高兴它有帮助。
    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多