【问题标题】:Do-While statement stuck in infinite loopDo-While 语句陷入无限循环
【发布时间】:2017-07-02 20:51:22
【问题描述】:

我已经盯着这个看了好几个小时了,我这辈子都无法弄清楚为什么运行时输出卡在一个循环中,就像这样(是的,我现在刚刚更正了估计的拼写):

您未来的孩子预计会长到 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 英寸

每次我写循环时都会发生这种情况。

//允许使用键盘 扫描器键盘输入 = 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("");
    } } }

【问题讨论】:

  • 如果你从不改变循环内的性别,它将如何退出。这不是一个编程问题,而是一个基本的逻辑问题。
  • 您需要使用if 而不是while
  • 如果您遵循 Java 命名约定,这将对每个人都有帮助。
  • 我在我正在做的事情中纠正了这一切。并不是我们都不知道它指的是什么。放松。这不像是最终确定的。

标签: java loops infinite-loop do-while


【解决方案1】:

在您的循环中,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);

【讨论】:

  • 我通常不会大写我的变量。我不知道我的大脑今天在做什么。 if else if 就像一个魅力。非常感谢。
  • 非常欢迎您 :) 我还向您展示了一个使用 do-while 的示例,它可以重复整个过程。
  • 是的,我确实看到了。再次,非常感谢你。我什至无法开始告诉你我是多么感激这一点。比我试图弄清楚这一点的东西更有意义!
【解决方案2】:

为了退出循环,您需要在循环执行所需次数后将条件变为假。

同样在上面的代码中,您似乎是在要执行的语句之间做出选择,而不是运行传统的循环。考虑使用“if”语句

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

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

如果您希望循环在打印输出语句“X”次后退出,您宁愿将条件基于计数变量。

因此,根据 Gender 变量做出选择,而打印语句则基于 count 变量。

//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++;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2022-11-20
    • 2020-05-07
    相关资源
    最近更新 更多