2017-07-02 38 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++; 
}