2013-04-11 70 views
1

我是初學者,所以請裸露在我身邊。我需要問三個薪酬水平。等級1必須至少爲65k,等級2必須至少爲60k。等級1也必須至少是等級2的110%。等級2也必須至少等於等級3的120%。我只從等級1和等級2開始,因爲如果我能夠弄清楚,我可以計算出等級3 。這是我有:重複數據驗證

do { 
     System.out.println("Please enter the full time employee payscale level 1 rate: "); 
     fullTimePay1 = input.nextInt(); 
    } while (fullTimePay1 < 65000); 

    do { 
     System.out.println("Please enter the full time employee payscale level 2 rate: "); 
     fullTimePay2 = input.nextInt(); 
     } while (fullTimePay2 < 60000); 

    do { 
     System.out.println("Please enter the full time employee payscale level 1 rate: "); 
     fullTimePay1 = input.nextInt(); 
     System.out.println("Please enter the full time employee payscale level 2 rate: "); 
     fullTimePay2 = input.nextInt(); 
    } while (fullTimePay1 < (fullTimePay2 * 1.1)); 

,但它只是重複一遍又一遍(我有這樣的代碼從未執行後打印語句)。任何人都可以告訴我爲什麼,也許可以幫助我的代碼?

+0

我將在調試器中遍歷您的代碼,以便了解您的程序在做什麼。我本以爲你只想要一個循環,一旦你有正確的答案,你就不想再問。 – 2013-04-11 08:25:23

+0

什麼是輸入,下一個int來自哪裏?你爲什麼重複獲得下一個int? – 2013-04-11 08:26:28

+0

這不是我的整個代碼,只是不能正常工作的部分。 – Chara 2013-04-11 08:28:24

回答

0

正如您的代碼所示,當我輸入例如100000和70000(兩次)循環終止;你可能會混淆哪個值應該更大(或者不要嘗試連續超過一次)?

您的代碼將無法正常工作,因爲您沒有對第三個循環執行65k和60k驗證,而且您還要求每次輸入兩次而不是一次(輸入正確時)(do-while檢查條件之後循環,而不是之前(這是一個while循環會做),所以第三個循環將要求用戶輸入,無論這些值是什麼)。

與下面的應該工作更換3個循環:

do { 
    do { 
    System.out.println("Please enter full time employee payscale level 1 rate:"); 
    fullTimePay1 = input.nextInt(); 
    } while (fullTimePay1 < 65000); 
    do { 
    System.out.println("Please enter full time employee payscale level 2 rate:"); 
    fullTimePay2 = input.nextInt(); 
    } while (fullTimePay2 < 60000); 
} while (fullTimePay1 < (fullTimePay2 * 1.1)); 

但驗證110%的第二個只會讓我更有意義,如果這是允許的:

do { 
    System.out.println("Please enter full time employee payscale level 1 rate:"); 
    fullTimePay1 = input.nextInt(); 
} while (fullTimePay1 < 66000); // 'cause min(1) = 1.1*min(2) = 1.1*60000 = 66000 
do { 
    System.out.println("Please enter full time employee payscale level 2 rate:"); 
    fullTimePay2 = input.nextInt(); 
} while (fullTimePay2 < 60000 || fullTimePay1 < (fullTimePay2 * 1.1)); 
+0

謝謝你,你的第一個解決方案完美無缺! – Chara 2013-04-11 09:19:51