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

huangapple go评论61阅读模式
英文:

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

问题

String[][] array = {{"Checkup", "60"},
                    {"Repairing tooth", "150"},
                    {"Cleaning", "30"}};  // 治疗项目菜单

String[] array2 = new String[10];  // 新数组,最多保存10个项目

int cost = 0;
int treatment = 0;

Scanner input = new Scanner(System.in);

System.out.println("控制" + " " + "1");
System.out.println("修复牙齿:" + " " + "2");
System.out.println("清洁:" + " " + "3");

int n = array.length;
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        System.out.println();
        treatment = input.nextInt();
        if (treatment == 1) {
            cost += Integer.parseInt(array[i][1]);
            System.out.print("到目前为止的总费用: " + cost);
        }
        if (treatment == 2) {
            cost += Integer.parseInt(array[i + 1][1]);
            System.out.print("到目前为止的总费用: " + cost);
        }
        if (treatment == 3) {
            cost += Integer.parseInt(array[i + 2][1]);
            System.out.print("到目前为止的总费用: " + cost);
        }
    }
}

从这里如何继续?我认为我需要将输入存储在新数组中,并在10个治疗项目后退出循环,或者在用户完成时添加一个选项来打印收据。

收据需要打印出所有选择的治疗项目以及每个项目的费用。我还需要添加一个变量来累加所有选择的治疗项目的总费用。

英文:

<!-- language: lang-java -->

String[][] array = {{&quot;Checkup&quot;, &quot;60&quot;},
{&quot;Repairing tooth&quot;, &quot;150&quot;},  
{&quot;Cleaning&quot;, &quot;30&quot;}};  // 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(&quot;Control&quot; + &quot; &quot; + &quot;1&quot;);
System.out.println(&quot;Repair tooth:&quot; + &quot; &quot; + &quot;2&quot;);
System.out.println(&quot;Cleaning:&quot; + &quot; &quot; + &quot;3&quot;);
int n = array.length;
for (int i=0; i&lt;n; i++) {
for (int j=0; i&lt;n ; j++) {
System.out.println();
treatment = input.nextInt();
if (treatment==1) {
cost += Integer.parseInt(array[i][1]);
System.out.print(&quot;Total cost so far: &quot; + cost);
}
if (treatment==2) {
cost += Integer.parseInt(array[i+1][1]);
System.out.print(&quot;Total cost so far: &quot; + cost);
}
if (treatment==3) {
cost += Integer.parseInt(array[i+2][1]);
System.out.print(&quot;Total cost so far: &quot; + cost);
}
}
}

How do I move on from here? I figured that I have to store the input in the new array and exit the loop after 10 treatments or add an option to the user to print out the receipt when they're done.

The receipt needs to print all the chosen treatments along with the cost for each individual treatment. I will also need to add a variable to add a total amount for all the chosen treatments.

答案1

得分: 0

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 treatments you opted for are:\n");
        System.out.println(treat);
        System.out.println("\n");

    }
}
英文:

Here it is what you are trying to do, As the treatments are fixed so you can just index them as 0, 1, 2. One thing you can do is to make a hashmap in which you can store the treatment name and its cost (String,int) every time the user wants to enter.
Look at the code below

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

huangapple
  • 本文由 发表于 2020年10月4日 22:02:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/64195583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定