java - While 循环没有相应地工作

标签 java loops arraylist while-loop switch-statement

以下代码要求用户输入他所消费的商品的描述、价格和数量。

有一个while循环询问他是否要输入更多项目!如果他这样做了,程序会要求插入其他元素的其他描述、价格和数量,等等。

如果他不想输入更多项目,则输出是他添加到数组中的所有项目以及帐单总额。

问题是:第一次运行 while 时,它​​可以工作,但第二次如果用户回答“y”,它会返回一个错误,就好像他从描述右边跳到了第二件商品的价格一样。如果用户键入描述,则会出现输入不匹配异常。

主类:

package com.company;

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

    ArrayList<Gastos> billArr = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    int qntItems = 0 , counter = 0;
    String ans;
    Gastos bill = new Gastos();

    while (qntItems == 0) {

        System.out.print("Want to input another item? Y/N: ");
        ans = input.nextLine();

        switch (ans){
            case "y":
                qntItems = 0;
                bill.setDescription();
                bill.setPrice();
                bill.setQuantity();
                bill.getTotal();
                billArr.add(bill);

                counter = counter + 1;
                break;

            case "n": qntItems = 1;
                    break;
            default: System.out.print("Invalid!");
                        System.out.println();
                        break;
        }
        input.close();

    }
    for (int i = 0; i < billArr.size();i++){
        System.out.print(bill.getDescription() + ", " + bill.getPrice() + ", " + bill.getQuantity() + ", " + "the total is: " + bill.getTotal());
    }

}
}

和 Gastos 类:

package com.company;

import java.util.Scanner;

public class Gastos {

private String description;
private double price, quantity, total;
private Scanner input = new Scanner(System.in);

public void setDescription(){
    System.out.print("Insert the item name: ");
    description = input.nextLine();
}

public void setPrice(){
    System.out.print("insert the item price: ");
    price = input.nextDouble();
}

public void setQuantity(){

    System.out.print("Insert the quantity: ");
    quantity = input.nextDouble();
}

public String getDescription(){
    return description;
}

public double getPrice() {
    return price;
}

public double getQuantity() {
    return quantity;
}

public double getTotal(){
    total = price * quantity;
    return total;
}

}

我该如何处理这个错误?

最佳答案

您的第二个循环中有一个错误。

应该是: System.out.print(billArr.get(i).getDescription()..... 或者简单地说:

for(Gastos b : billArr){ 
    System.out.print(b.getDescription()) 
}

更新 1:另一个错误是您在第一个循环结束时关闭了扫描仪。将 input.close(); 移至循环外部或 case "n" 内。

更新 2:您还有另一个问题,每次输入新的详细信息时都需要重新初始化 Gastos。因此,您需要在 case "y": 之后执行 Gastos bill = new Gastos(); ,并将其从 while 循环之前初始化的位置删除。您的主要内容应如下所示:

public static void main(String[] args) {

    ArrayList<Gastos> billArr = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    int qntItems = 0 , counter = 0;
    String ans;

    while (qntItems == 0) {

        System.out.print("Want to input another item? Y/N: ");
        ans = input.nextLine();

        switch (ans){
            case "y":
                Gastos bill = new Gastos();
                qntItems = 0;
                bill.setDescription();
                bill.setPrice();
                bill.setQuantity();
                bill.getTotal();
                billArr.add(bill);

                counter = counter + 1;
                break;

            case "n": qntItems = 1;
                input.close();
                break;
            default: System.out.print("Invalid!");
                System.out.println();
                break;
        }
    }
    for (Gastos bill : billArr){
        System.out.print(bill.getDescription() + ", " + bill.getPrice() + ", " + bill.getQuantity() + ", " + "the total is: " + bill.getTotal());
    }
}

我认为你需要花一些时间调试和理解java的对象是如何工作的。这些都是基本错误,应该很容易发现。

关于java - While 循环没有相应地工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037271/

相关文章:

javascript - 显示其选项卡的图标

python - 从使用Python中的groupby itertools创建的词典列表中删除重复项

java - 如何创建一个方法来计算数组中的字符数

java - java中for循环的最小和最大结果

java - 如何在 Play 中保存 ArrayList?

java - 比较二维数组中的 2 个元素

java - 如何将集合集合的项目与当前日期进行比较?

java - apache-arrow 是否可以使用 Java API 在单独的线程中创建 vector 的一部分?

java - 如何在一个项目中同时使用Androidx库和支持库?

java - 在 Java 中创建一个 .eml(电子邮件)文件