我一直在盯着這看起來像幾個小時,我不能爲我的生活弄清楚爲什麼運行時輸出卡在循環中,就像這樣(是的,我現在只需更正估計的拼寫):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命名約定,它確實可以幫助所有人。 –