我一直在盯着这看起来像几个小时,我不能为我的生活弄清楚为什么运行时输出卡在循环中,就像这样(是的,我现在只需更正估计的拼写):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(""); } } }
如果你从来没有改变环路内的性别,如何将它曾经退出。这不是一个编程问题,而是一个基本的逻辑问题。 –
您需要使用'if'而不是'while'。 –
如果您遵循Java命名约定,它确实可以帮助所有人。 –