2012-02-21 333 views
4

嗨,我有麻煩讓我的程序正常運行。我能夠清除任何語法錯誤,但現在我已經發布了我的輸出。while循環嵌套的if/else語句

首先,在第一個IF語句中,它會提示要求人員同時輸入他們的姓名和部門,以便在輸出時,名稱是空白的,只有部門有輸入。我認爲這是整個IF語句的事情,因爲如果我將「String name」更改爲input.next,則名稱提示正確,但將dept和totalHrsWkd合併在一起。

此外,當測試我的程序時,它輸入totalHrsWkd的負數時崩潰。它會在一行中顯示兩條打印語句,然後崩潰JCreator。

我很感激任何關於此事的幫助,謝謝!

public static void main(String[] args) 
    { 
    // TODO code application logic here 

    int attempt = 1, employeeID = 0; 
    double hoursWorked = 0.0; 
    double overtimeHrs = 0.0; 
    double totalHrsWkd = 0.0; 

    Scanner input = new Scanner(System.in); 

    while(attempt < 4) 
    { 
     System.out.println("Enter your employee ID: "); 
     employeeID = input.nextInt(); 

     if(employeeID == 12345678) 
     { 
     System.out.printf("Enter your name: "); 
     String name = input.nextLine(); 

    System.out.printf("Enter your department: "); 
     String dept = input.nextLine(); 

    System.out.printf("Enter your hours worked including overtime: "); 
     totalHrsWkd = input.nextDouble(); 

     while(totalHrsWkd < 0) 
      { 
      System.out.printf("Try again! Hours worked cannot be negative."); 

      System.out.printf("Enter your hours worked including overtime: "); 
      } 

      overtimeHrs = totalHrsWkd - 40; 
      hoursWorked = totalHrsWkd - overtimeHrs; 

      if(overtimeHrs <= 0) 
      { 
      } 
      else if(overtimeHrs == 0) 
      { 
      } 
      else if(hoursWorked == totalHrsWkd) 
      { 
      } 
      else if(hoursWorked == 40) 
      { 
      } 
     System.out.printf("Name: %s\n" + "Dept: %s\n" + 
        "Hours Worked: %.2f\n" + "Overtime Hours: %.2f\n" 
        + "Total Hours Worked: %.2f\n", name, dept, 
        hoursWorked, overtimeHrs, totalHrsWkd); 

     attempt = 3; 
     } 
    else 
    { 
     if(attempt < 3) 
     { 
     System.out.println("Invalid ID! Try again." ); 
     } 
     else 
     { 
     System.out.println("Invalid ID! 0 attempts left. Exiting program!"); 
     } 

     } 

    ++attempt; 
    } 
    System.exit(0); 
} 
} 
+0

'totalHrsWkd = input.nextDouble();'應該放在while循環中。否則,你只是在一個無限循環中運行,沒有任何變化'totalHrsWkd' – biziclop 2012-02-21 20:49:18

+0

你爲什麼選擇使用'Scanner'?這幾乎不適用於交互式程序(並導致像你在這裏看到的問題)。相反,使用['BufferedReader.readLine'](http://docs.oracle.com/javase/1.5.0/docs/api/java/io/BufferedReader.html)。 – 2012-02-21 20:50:23

+0

是的,這是我的Java 1類的編程任務。本質上,老師給我們提供了僞代碼,我們需要編寫程序。到目前爲止,我們只處理Scanner類。我甚至沒有在我們的書中遇到過BufferedReader.readLine。 – Abweichung 2012-02-21 20:54:21

回答

3

問題是

employeeID = input.nextInt(); 

讀取從輸入流中的數字序列的下一個。但是,爲了實際輸入員工ID,您還必須按Enter鍵。回車按鍵仍然在等待輸入,所以當你下次調用

String name = input.nextLine(); 

Scanner類變爲「哦,這裏是一個輸入按鍵,我猜測用戶想要一個空的名字。」你甚至沒有機會輸入一個名字,這就是爲什麼你的程序似乎在同一時間提出兩個問題(名稱和部門)。

如果您希望繼續使用Scanner類,我建議避開諸如nextInt()之類的方法,並始終使用nextLine()來代替。當然,您將不得不使用類似 parseInt()將用戶鍵入的數字轉換爲Java整數。

+0

那麼我需要先把它作爲一個字符串嗎?如: String ID = System.out.println(「輸入您的員工ID:」); int employeeID = Int。parseInt(ID); 如果是這樣,我會得到不兼容的類型錯誤以及找不到符號變量Int。 – Abweichung 2012-02-21 22:02:23

+0

不完全是,你試圖把'println()'的返回值賦給一個'String'。嘗試:'String employeeIDStr = input.nextLine(); int employeeID = Integer.parseInt(employeeIDStr);'還要注意'Integer'類的使用,而不是'Int'。 – 2012-02-21 22:08:21

+0

終於!真的很抱歉打擾你的初學者問題,但我基本上留下來爲自己照顧,因爲她花費課時講授幻燈片。 我必須從頭開始編寫一個程序,所以我想先完成這個程序以幫助充當模板。 再次感謝! – Abweichung 2012-02-21 22:15:20

2
attempt = 3; // Seems suspicious 

attempt++; // I think it's more appropriate in loops of this kind than ++attempt 
1

當你的程序在totalHrsWkd小於0時不工作的原因是因爲下面的代碼創建了一個無限循環。

while (totalHrsWkd < 0) 
      { 
      System.out.printf("Try again! Hours worked cannot be negative."); 

      System.out.printf("Enter your hours worked including overtime: "); 
      } 

您應該讀取此循環中工作的小時數。

+0

現在我明白了上面的含義,缺少totalHrsWkd = input.nextDouble(); 我需要把它放在我的第二個提示下。這解決了我的無限循環問題,但仍在解決其他問題,謝謝! – Abweichung 2012-02-21 21:54:31