2011-02-27 2 views
0

Ok 사용자가 특정 키를 입력하면 프로그램 섹션을 이전 섹션으로 되돌릴 수있는 방법을 찾는 데 문제가 있습니다. 예를 들어, 언제든지 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

'그들이 어떤 시간에 프로그램이 체중 section'에 걸릴 필요가 w를 공격하는 경우 - 당신은이 작업을 수행하는 기능을 다시 전화해야 할 필요가 있다고 생각합니다. 키보드 이벤트가 트리거되고 눌려진 키 문자를 확인하고 해당 메서드를 호출합니다. – Mahesh

+0

빠른 콜백 예제를 보여 주시겠습니까? 교수님은 아직 우리에게이 사실을 알려주지 못했으며 예제를 찾는 데 어려움을 겪고 있습니다. – Brad

답변

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 

종료 주()

+0

그게 좋은 해결책 인 것 같아. 고맙습니다. – Brad

+0

훨씬 더 깨끗합니다. 나는 포스터가 명령 패턴을 참고 자료로보고 싶다고 생각했다. – mozillanerd

+0

나는 이것을 switch 문에 넣었지만, 다음과 같이 보였다. 아를 입력하거나 아무것도하지 않는다. – Brad