2017-03-27 14 views
2

次のコードは、消費したアイテムの説明、価格、量を入力するようにユーザーに求めます。Whileループはそれに応じて動作しません

もっと多くの項目を入力したいかどうかを尋ねるwhileループがあります。もしそうであれば、プログラムは別の説明、他の品目の価格と数量などを挿入するよう求めます。

もっと多くの項目を入力したくない場合、出力は配列に追加したすべての項目と請求書の合計です。

問題は:whileが実行されて初めて動作しますが、ユーザーが "y"で答えると2回目に、説明の右から2番目の価格にジャンプしたようにエラーが返されます項目。ユーザーが説明を入力すると、入力ミスマッチ例外が発生します。

メインクラス:

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; 
} 

} 

どのように私はこのエラーを処理することができますか?

+2

2番目のループをよく見てください。 'System.out.print(billArr.get(i).getDescription().....'または単に 'for(Gastos b:billArr){System.out.print(b.getDescription() )}} '' – zengr

+0

最後の 'for'ループは' for(Gastos bill:billArr){' – 4castle

答えて

3

2番目のループにバグがあります。

それは次のようになります。 System.out.print(billArr.get(i).getDescription()..... または単に置く:

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

アップデート1:別のエラーは、あなたが最初のループの最後でScannerを閉じています。ループの外側またはcase "n"の内側でinput.close();を移動します。

更新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のオブジェクトの仕組みを理解する必要があると思います。これらは簡単に捕らえるべき基本的な誤りです。

+0

になります実際には2番目のループが動作します!' while(qntItems == 0){' – RooseveltJr

+0

エラー: – zengr

+0

コンソールに「別のアイテムを入力しますか?」Y/N:y アイテム名を挿入:アイテム価格を挿入すると「価格が説明を上書きしたようです。私は説明を挿入すると、 'java.util.InputMismatchException'を取得します。 – RooseveltJr

関連する問題