2014-02-24 4 views
0

사용자 입력에 따라 반복해서 프로그램을 반복 할 수 있기를 원합니다. 당신은전체 프로그램을 어떻게 루프합니까?

import java.util.*; 

public class Lab4 { 
    public static void main(String[] args){ 
     System.out.println("Body Fat Calculator"); 

     double A1,A2,A3,A4,A5,B; //female 
     double a1,a2,b; //male 
     double bodyWeight,wristMeasurement,waistMeasurement,hipMeasurement,forearmMeasurement; //both 
     double bodyFat,bodyFatpercent; //both 

     Scanner body = new Scanner (System.in); 

     System.out.println ("Enter Gender (m/f): "); 
     char gender = body.nextLine().charAt(0); 

     while ((gender != 'm') && (gender != 'f')) { 
      System.out.println ("Unknown gender, Enter gender again (m/f); "); 
      gender = body.nextLine().charAt(0); 
     } 

     do { 
      if (gender == 'm') { 
       System.out.println ("Enter Your Body Weight: "); 
       bodyWeight = body.nextInt(); 

       System.out.println ("Enter Your Waist Measurement: "); 
       waistMeasurement = body.nextDouble(); 

       a1 = (bodyWeight * 1.082) + 94.42; //male formula 
       a2 = waistMeasurement * 4.15; 
       b = a1 - a2; 

       bodyFat = bodyWeight - b; 
       bodyFatpercent = bodyFat * 100/bodyWeight; 

       System.out.printf ("%s %.2f%%%n", "Your Bodyfat Percentage is: ", bodyFatpercent); 
      } 

      else { 

       System.out.println ("Enter Your Body Weight: "); 
       bodyWeight = body.nextInt(); 

       System.out.println ("Enter Your Wrist Measurement: "); 
       wristMeasurement = body.nextDouble(); 

       System.out.println ("Enter Your Waist Measurement: "); 
       waistMeasurement = body.nextDouble(); 

       System.out.println ("Enter Your Hip Measurement: "); 
       hipMeasurement = body.nextDouble(); 

       System.out.println ("Forearm Your Measurement: "); 
       forearmMeasurement = body.nextDouble(); 

       A1 = (bodyWeight * 0.732) + 8.987; // female formula 
       A2 = wristMeasurement/3.14; //at fullest point 
       A3 = waistMeasurement * 0.157; //at navel 
       A4 = hipMeasurement * 0.249; //at fullest point 
       A5 = forearmMeasurement * 0.434; //at fullest point 
       B = A1 + A2 - A3 - A4 + A5; 

       bodyFat = bodyWeight - B; 
       bodyFatpercent = bodyFat * 100/bodyWeight; 
       System.out.printf ("%s %.2f%%%n", "Your Bodyfat Percentage is: ", bodyFatpercent); 
      } 
     } 
     while (true){ 
      System.out.println ("Would You Like to Use the Calculator Again (y/n)? "); 
      char answer = body.nextLine(); 
      // should loop here to run the program again 
     }  
    } 
+0

루프 구조 (예 :'for' 또는'while')를 사용해 보셨습니까? –

답변

0

는 루프에하고자하는 전체 코드와 while 또는 for 루프를 사용하여 ... 아래에있는 내 코드를보고이 작업을 수행 할 수있는 방법을 이해하는 데 도움이 시겠어요. 예를 들면 :

IsConditionTrue() returns a boolean value depending on the termination condition of your loop. 

이 조건이 루프의 본문에 확인하는 사용하여 변수를 업데이트하는 것을 잊지 마세요

while(IsConditionTrue()){ 
    System.out.println ("Enter Gender (m/f): "); 
    char gender = body.nextLine().charAt(0); 
    // more code 
    // code to update the variable using which the condition is checked 
} 

.

3
그것은 또 다시 그 메소드를 호출 한 후 별도의 방법으로 밖으로 코드를 이동하고, 비교적 쉽게 수행 할 수 있습니다

은 ...

public void runMyCode(){ 
    // Put all your code in here 
    } 

public static void main(String[] args){ 
    boolean runAgain = true; // start off being true, so it will enter the loop for the first time 

    while(runAgain){ 
    runMyCode(); // runs your code 

    // when your code has been run once, it will come back here and ask this question 
    System.out.println ("Would You Like to Use the Calculator Again (y/n)? "); 
    char answer = body.nextLine(); 

    // we change the value of the boolean so that the while loop will repeat again only if the user enters 'y' 
    if (answer == 'y'){ 
     runAgain = true; 
     } 
    else { 
     runAgain = false; 
     } 
    } 
    } 

main() 방법은 바로 프로그램의 반복을 제어합니다. 다른 모든 질문 및 처리는 runMyCode() 방법에서 발생합니다.

+0

당신은 Java 학습에 많은 도움이되었습니다. 고마워. – jazzboy

관련 문제