One way to estimate the adult height of a child is to use the following formula,which uses the height of the parents:Hmale_child=((Hmother*13/12)+Hfather)/2Hfemale_child=((Hfather*12/13)+Hmother)/2All heights are in inches. Write a program that takes as input the gender of the child,the height of the mother in inches,and the height of the father in inches,and out-puts the estimated adult height of the child in inches. The program should allow the user to enter a new set of values, ouputting the predicted height, until the userdecides to exit. The user should be able to input the heights in feet and inches,andthe program should output the estimated height of the child in feet and inches. Usethe integer data type to store the heights.煩請高手幫忙解答
2006-06-01 18:21:06 · 2 個解答 · 發問者 23歲九局下半 4 in 電腦與網際網路 ➔ 程式設計
首先翻譯一下題目 要估算小朋友長大後的身高,下面的方程式是其中的一個方法,它是利用父母親的身高。 小男孩身高 = ((母親身高 * 13 / 12) + 父親身高) / 2 小女孩身高 = ((父親身高 * 12 / 13) + 母親身高) / 2 此方程式的身高是以英吋為準。實作一個程式,使用者輸入小朋友的性別,雙親的身高(英吋),計算後印出小朋友長大後預估的身高(英吋)。程式必須讓使用者能繼續輸入新的資料,印出其預估身高,直到使用者決定結束。使用者輸入的身高須以英呎及英吋做為單位,而程式所印出的小朋友預計身高也須以英呎及英吋為單位。利用整數型態來儲存身高。以下是我的做法,請參考。import java.io.*;import java.util.*;public class EstimatedHeight { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); boolean again = true; while (again) { System.out.println(); System.out.print("Input the child's gender (m/f):"); String gender = br.readLine(); System.out.print("Input mother's height in feet and inches (i.e. 5'5\"): "); String mHeightStr = br.readLine(); System.out.print("Input father's height in feet and inches (i.e. 5'5\"): "); String fHeightStr = br.readLine(); // 利用 StringTokenizer 轉換單位,如將 5'5" 轉換成 5*12+5 = 65 英吋 StringTokenizer tokenizer = new StringTokenizer(mHeightStr, "\'\""); int mHeight = 12 * Integer.parseInt(tokenizer.nextToken()); if (tokenizer.hasMoreTokens()) { mHeight += Integer.parseInt(tokenizer.nextToken()); } tokenizer = new StringTokenizer(fHeightStr, "\'\""); int fHeight = 12 * Integer.parseInt(tokenizer.nextToken()); if (tokenizer.hasMoreTokens()) { fHeight += Integer.parseInt(tokenizer.nextToken()); } int eHeight = 0; if (gender.equalsIgnoreCase("m")) { eHeight = ((mHeight * 13 / 12) + fHeight) / 2; } else if (gender.equalsIgnoreCase("f")) { eHeight = ((fHeight * 12 / 13) + mHeight) / 2; } System.out.println("The child's estimated adult height is " + (eHeight/12) + "\'" + (eHeight%12) + "\""); System.out.println(); System.out.print("Continue? (y/n): "); String line = br.readLine(); if (line.equalsIgnoreCase("f")) again = !again; } }}
2006-06-02 16:18:20 補充:
對不起,最後一個 statement 有誤,請更正為
if (line.equalsIgnoreCase("n")) again = !again;
2006-06-02 12:13:27 · answer #1 · answered by ? 7 · 0⤊ 0⤋
回答者很用心!!值得投正面評價!!^^
2006-06-03 14:46:58 · answer #2 · answered by 賴小賴 2 · 0⤊ 0⤋