【发布时间】: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