2011-02-27 35 views
0

好吧我試圖找出如何讓我的程序循環返回到前一節如果用戶輸入某個鍵時遇到問題。例如,如果他們在任何時間點擊w,則程序需要將重量部分放到重量部分,以便他們可以輸入新的重量,並且h爲高度。如果你們可以給我一些建議,我將不勝感激。 謝謝你們。)循環和重新分配值

package Assignments; 

進口的java.util。*; 公共類assignment3 {

public static void main(String[] args) { 

    //Scanner 
    Scanner stdIn = new Scanner(System.in); 

    //Variables 
    final double METERS_TO_CM = 100; // The constant to convert meters to centimeters 
    final double BSA_CONSTANT = 3600; // The constant to divide by for bsa 
    double bmi;      // Body Mass Index 
    double weight;      // Weight in kilograms 
    double height;      // Height in meters 
    String classification;    // Classifies the user into BMI categories 
    double bsa;      // Body surface area 



    System.out.print("Welcome to the BMI and BSA Calculator to begin enter weight in kilograms."); 
    weight = stdIn.nextDouble(); 
    System.out.print("Enter height in meters: "); 
    height = stdIn.nextDouble(); 
    bmi = weight/(height*height); 
    bsa = Math.sqrt(((height*METERS_TO_CM)*weight)/BSA_CONSTANT); 


     if (bmi < 18.5) 
     { 
      classification = "Underweight"; 
     } 
     else if (bmi < 25) 
     { 
      classification = "Normal"; 
     } 
     else if (bmi < 30) 
     { 
      classification = "Overweight"; 
     } 
     else 
     { 
      classification = "Obese";} 


      System.out.println("Choose Options below to set height and weight"); 
      System.out.println("Your classification is: " + classification); 
      System.out.println("(H)eight: " + height + " meters"); 
      System.out.println("(W)eight: " + weight + " kilograms"); 
      System.out.printf("BMI: %.1f\n", bmi); 
      System.out.printf("BSA: %.2f\n", bsa); 
      System.out.println("(Q)uit"); 

      String response = stdIn.next(); 

      switch (response.charAt(0)) { 
      case 'w': response = "Enter new weight: "; 
      weight = stdIn.nextDouble(); 
      System.out.println("Choose Options below to set height and weight"); 
      System.out.println("Your classification is: " + classification); 
      System.out.println("(H)eight: " + height + " meters"); 
      System.out.println("(W)eight: " + weight + " kilograms"); 
      System.out.printf("BMI: %.1f\n", bmi); 
      System.out.printf("BSA: %.2f\n", bsa); 
      System.out.println("(Q)uit"); break; 

      case 'h': response = "Enter new height"; 
      height = stdIn.nextDouble(); 
      System.out.println("Choose Options below to set height and weight"); 
      System.out.println("Your classification is: " + classification); 
      System.out.println("(H)eight: " + height + " meters"); 
      System.out.println("(W)eight: " + weight + " kilograms"); 
      System.out.printf("BMI: %.1f\n", bmi); 
      System.out.printf("BSA: %.2f\n", bsa); 
      System.out.println("(Q)uit"); break; 

      case 'q': System.exit(0); 

      default: 
       System.out.println (response + "Is not a valid option please try again"); 

      } 


     } 









} 
+0

',如果他們打W¯¯隨時方案需要採取的重量section' - 我認爲你需要有回調函數來做到這一點。觸發鍵盤事件並檢查按下的鍵字符並調用相應的方法。 – Mahesh 2011-02-27 00:48:22

+0

你介意給我一個回調的例子嗎?我的教授還沒有教過我們,我無法找到一個例子。 – Brad 2011-02-27 00:57:35

回答

2

而不是做這一切在一個巨大的功能,你可以登錄分離出成更小的功能。這裏的輪廓

污穢函數來獲取重量輸入:雙getWeight()

污穢函數來獲得高度輸入:雙的getHeight()

定義函數來算一算。

定義顯示結果的函數。

main() 
    getWeight() 
    getHeight() 
    doMath() 
    showResult() 

    Loop 
    show options (H/W/Q) 
    switch 
     case H: 
     getHeight() 
     doMath() 
     showResult() 
     case W 
     getWeight() 
     doMath() 
     showResult() 
     case Q 
     Exit program 
    end switch 
    end Loop 

末的main()

+0

這看起來像一個偉大的解決方案,我會嘗試。謝謝 – Brad 2011-02-27 01:06:42

+0

更清潔。我在想,這個海報可能希望看到命令模式作爲參考http://www.javaworld.com/javatips/jw-javatip68.html – mozillanerd 2011-02-27 01:14:13

+0

好吧,我把它放在一個switch語句中,它看起來像這樣,但是當我進入啊或者它什麼都不做 – Brad 2011-02-27 01:57:59