【问题标题】:While loop not working accordingly虽然循环没有相应地工作
【发布时间】:2017-08-19 14:54:50
【问题描述】:

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

有一个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()) }
  • 最后一个for 循环可以是for (Gastos bill : billArr) {

标签: java loops arraylist while-loop switch-statement


【解决方案1】:

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

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

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

更新 1:另一个错误是您在第一个循环结束时关闭了 Scanner。将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 的对象是如何工作的。这些是应该很容易发现的基本错误。

【讨论】:

  • 实际上第二个循环有效!我的意思是while (qntItems == 0) { 第二次运行,它得到一个错误。
  • 在控制台上出现以下Want to input another item? Y/N: y Insert the item name: insert the item price: ,好像价格覆盖了描述。如果我插入描述,它会得到一个java.util.InputMismatchException
  • 非常感谢。我是初学者,打算花点时间调试一下。
猜你喜欢
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-23
  • 2021-12-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多