如何在do while循环中存储所有用户输入,并在Java程序中调用它们以供稍后打印?

mnemlml8  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(100)

我必须写一个Java程序做while循环。基本上它就像自助结帐登记在杂货店。首先程序提示用户输入项目1的名称和价格,在我们输入后,它问“你想添加更多吗?”并提示用户。如果我们说是,然后它重复,添加项目2**(项目编号递增)**名称和价格。因此,它是这样的,直到我们说“否”或直到输入10个项目。然后,程序打印所有项目编号对应它们的名称。和价格。还计算所有项目的总额。我的代码只打印最后一个项目的编号,名称,价格和总额。我不知道如何合并数组来完成。示例应该如何看:

_output: Enter Item1 and its price:_

_input: Tomatoes_

_input: 5.5_

_output: Add one more item?_

_input: yes_

_output: Enter Item2 and its price:_

_input: Cheese_

_input: 3.5_

_output: Add one more item?_

_input: yes_

_output: Enter Item3 and its price:_

_input: Apples_

_input: 6.3_

_output: Add one more item?_

_input: no_

_output: Item1: Tomatoes Price: 5.5, Item2: Cheese Price: 3.5, Item3: Apples Price: 6.3_

_output: Total price: 15.3_

这是我的代码。它只打印最后一个项目的编号,名称和总价格。但不能设法打印所有的一个接一个的例子。

Scanner scan = new Scanner(System.in);
        
         String item;
        double price;
        int i=1;
        double total=0;
        String quest;
        String items [] = {}; 
        do {
            System.out.println("Enter item " + i + " and it's price:");
            item = scan.next();
            price=scan.nextDouble();
            i++;
            total=total+price;
           
           
           
            System.out.println("Add one more item?");
            quest=scan.next();
        } while (quest.equalsIgnoreCase("yes")) ; {
            System.out.println("Item "+i+": " + item + " Price: " + price + ",");
            System.out.println("Total price: " + total);
        }
vi4fp9gy

vi4fp9gy1#

您需要将每个输入储存在array或集合中,例如ArrayList
如果选择使用数组,可以询问用户要输入多少项,然后使用该值作为数组的长度:

System.out.print("Enter the number of items in your cart: ");
int arraySize = scan.nextInt();

String[] itemNames = new String[arraySize];
double[] itemPrices = new double[arraySize];

如果您决定使用列表,则不需要向用户询问列表大小,因为列表是动态扩展数据类型的:

List<String> itemNames = new ArrayList<String>();
List<Double> itemPrices = new ArrayList<Double>();

然后,对于循环的每次迭代,将值保存到数组/列表中:

System.out.print("Enter the number of items in your cart: ");
int arraySize = scan.nextInt();

String[] itemNames = new String[arraySize];
double[] itemPrices = new double[arraySize];

for(int i = 0; i < ararySize; i++) {
    System.out.println("Enter item " + (i + 1) + " and it's price:");

    itemNames[i] = scan.next();
    itemPrices[i] = scan.nextDouble();

    // ...    
}
// ...

或者对于列表方法:

List<String> itemNames = new ArrayList<String>();
List<Double> itemPrices = new ArrayList<Double>();

do {
    System.out.println("Enter item " + i + " and it's price:");

    itemNames.add(scan.next());
    itemPrices.add(scan.nextDouble());

    // ...
} while (quest.equalsIgnoreCase("yes")); {
    // ...
}

然后你可以遍历列表/数组来打印输出:

for(int i = 0; i < itemNames.length; i++) {
    String name = itemNames[i];
    double price = itemPrices[i];
    System.out.println("Item " + i + ": " + name + " Price: " + price + ",");
}

列表:

for(int i = 0; i < itemNames.size(); i++) {
    String name = itemNames.get(i);
    double price = itemPrices.get(i);
    System.out.println("Item " + i + ": " + name + " Price: " + price + ",");
}

相关问题