简体   繁体   中英

How do i loop my whole program?

I want to be able to loop my program over and over again, depending on the user input. Could you please look at my code below and help me to understand how this can be done...

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
        }      
    }

It can be done relatively easily by moving your code out into a separate method, and then calling that method over and over again...

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;
      }
    }
  }

The main() method just controls the repeating of your program. All the other questions and processing occurs in the runMyCode() method.

Use a while or for loop with the entire code which you would like to loop. For example:

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 
}

where

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

Do not forget to update the variable using which the condition is checked in the body of the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM