2017-07-02 95 views
-2

我一直在盯着這看起來像幾個小時,我不能爲我的生活弄清楚爲什麼運行時輸出卡在循環中,就像這樣(是的,我現在只需更正估計的拼寫):Do-While語句卡在無限循環中

您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 您未來的孩子可以長到4英尺和10英寸。 你的未來的孩子被認爲長到4英尺和10英寸

每次我寫循環時都會發生這種情況。

//允許使用鍵盤 Scanner keyboardInput = new Scanner(System.in);

//Allows user to input numbers 
    System.out.println("Enter the gender of your future child. Use 1 for Female and 0 for Male: "); 
    int Gender = keyboardInput.nextInt(); 
    System.out.println("Enter the height in feet, then in inches of the mom: "); 
    int MomHeight = keyboardInput.nextInt(); 
    System.out.println("Enter the height in feet, then the height in inches of the dad: "); 
    int DadHeight = keyboardInput.nextInt(); 

    int female; 
    int male; 
    int HeightFeet; 
    int HeightInches; 

    DecimalFormat feet = new DecimalFormat("#0"); 
    DecimalFormat inches = new DecimalFormat("#0"); 

    //Loop statements 
    while (Gender == 0) 
    { 
     male = (MomHeight * 13/12 + DadHeight)/2; 
     HeightFeet = male/12; 
     HeightInches = male % 12; 

    System.out.print("Your future child is estimated to grow to " + feet.format(HeightFeet)); 
    System.out.print(" feet and " + inches.format(HeightInches)); 
    System.out.print(" inches."); 
    System.out.println(""); 
    } 

    while (Gender == 1) 
    { 
     female = (DadHeight * 12 /13 + MomHeight) /2; 
     HeightFeet = female/12; 
     HeightInches= female % 12; 

    System.out.print("Your future child is estmated to grow to " + feet.format(HeightFeet)); 
    System.out.print(" feet and " + inches.format(HeightInches)); 
    System.out.print(" inches."); 
    System.out.println(""); 
    } } } 
+3

如果你從來沒有改變環路內的性別,如何將它曾經退出。這不是一個編程問題,而是一個基本的邏輯問題。 –

+0

您需要使用'if'而不是'while'。 –

+0

如果您遵循Java命名約定,它確實可以幫助所有人。 –

回答

3

在你的循環,Gender是永遠不會改變。所以你永遠循環。
現在,我認爲你不需要while聲明。
if else if聲明將是可取的,因爲您不需要從用戶的新輸入循環,但您想要根據特定條件(男性或女性性別)應用處理。

順便說一句,您應該爲您的變量gender而不是Gender尊重的Java命名約定:

if (gender == 0){ 
     ... 
} 

else if (gender == 1){ 
     ... 
} 

如果你想對所有的處理多次重複,你可以使用在所有的循環:

boolean again = false; 
do{ 
     if (gender == 0){ 
      ... 
     } 

     else if (gender == 1){ 
      ... 
     } 
     ... 
     again = keyboardInput.nextBoolean(); 

} while (again); 
+0

我通常不會大寫我的變量。我不知道我今天的大腦在做什麼。 的,如果其他人如果工作就像一個魅力。非常感謝。 – Stephanie

+0

非常歡迎您:)我還向您展示了一個使用'do-while'來重複整個過程的例子。 – davidxxx

+0

是的,我確實看到了。再一次,非常感謝你。我甚至無法開始告訴你我對此表示讚賞。比我想要解決的問題更有意義! – Stephanie

0

爲了退出循環,需要在循環執行完所需次數後變爲false的條件。

同樣在上面的代碼中,您似乎在要執行的語句之間做出選擇,而不是運行傳統的循環。考慮使用「if」語句

while (Gender == 0){ 
//Do this 
} 

while (Gender == 1){ 
//Do this instead 
} 

如果你想在循環退出打印輸出語句「X」次後,你寧願立足於一個計數變量的條件。

所以作出選擇是基於性別變量和打印報表是基於計數變量。

//Assume print is to be done 2 times 

int count = 1; 

if(Gender == 0){ 
//Female selection 
while(count < 3){ 
// Execute female code 
count++; 
} 
else if(Gender == 1){ 
//Male selection 
while(count < 3){ 
// Execute male code 
count++; 
}