java - 我如何循环我的整个程序?

标签 java loops while-loop

我希望能够根据用户输入一遍又一遍地循环我的程序。您能否查看下面的代码并帮助我了解如何做到这一点...

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

最佳答案

通过将代码移至单独的方法中,然后一遍又一遍地调用该方法,可以相对轻松地完成此操作...

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() 方法中。

关于java - 我如何循环我的整个程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978065/

相关文章:

c# - java和c#中泛型的语法差异

java - 使用具有默认值的 Quartz 调度程序的计划作业

php - PHP while 循环的第一行不回显到屏幕

python - 为什么 while 循环中的缩进会产生问题?

java - 为什么我在 Java 中收到 "missing return statement"错误,即使我在第二类中有 return 语句和线程主错误中的异常?

java - 信号协议(protocol)问题

javascript - 如何使用循环为多个元素构建 Javascript/Jquery?

python - 嵌套循环来创建模式

java - 正确的循环方式以避免创建双对象

php - MySQL 在同一查询中从两个表中进行选择