2017-12-03 14 views
-1

私のプログラムは、誰かにシリアル番号とオブジェクトの価格を入力するように求められています。ここでwhileループは最初の反復後に最初の入力スキャナをスキップしていますか?

クラスドライバ

import java.io.*; 
import java.util.Scanner; 

public class InventoryDriver { 
    public static void main(String[] args)throws IOException { 
     Inventory storeItem = new Inventory(" ", 1); 
     PrintWriter storeFile = new PrintWriter("StoreInventory.txt"); 
     Scanner scan = new Scanner(System.in); 
     boolean running = true; 

     String itemNum = " "; 
     double originalPrice = 1; 

     while (running) { 

      System.out.println("Enter the item number or enter 0 when you are done."); 
      itemNum = scan.nextLine(); 

      storeItem.setItemNum(itemNum); 

      storeFile.println(itemNum); 

      if(itemNum.equals("0")) break; 


      System.out.println("Enter the original price or enter 0 when you are done."); 
      originalPrice = scan.nextDouble(); 

      storeItem.setOriginalPrice(originalPrice); 

      storeFile.println(originalPrice); 

      if(originalPrice == 0) break; 
     } 

     //create a scanner variable named myFile 
     Scanner myFile = new Scanner(new File("StoreInventory.txt")); 

     //Read my data from my file into the variablles 
     //itemNum = myFile.nextLine(); 
     //originalPrice = myFile.nextDouble(); 

     printSaleData(myFile, storeItem); 

     //Close the file 
     myFile.close(); 
     storeFile.close(); 
    } 
} 

は、この出力

Enter the item number or enter 0 when you are done. 
    input 

    Enter the original price or enter 0 when you are done. 
    input 

と完全に最初の時間を働いた後である。しかし、ループが戻ったとき、それはこの

Enter the item number or enter 0 when you are done. 
    input 

    Enter the original price or enter 0 when you are done. 
    input 

    Enter the item number or enter 0 when you are done. 
    Enter the original price or enter 0 when you are done. 
    input 

何を印刷します私のコードについては、最初の後にループを変更しますか?
P.ループを壊しても、プログラム全体が停止し、メインモジュールに「printSaleData」サブモジュールを呼び出さない。

+1

これらの結果を得るために入力した内容を明確にしてください。あなたが探している出力はどれくらいですか? – Keara

+0

'itemNum = scan.nextInt();'に 'itemNum = scan.nextLine();'を変更するかもしれません。 – icarumbas

答えて

0

scan.nextLineをscan.next()に更新します。これでやります。

関連する問題