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 ((gender == 'm') && (gender != 'm'));
System.out.println ("Would You Like to Use the Calculator Again (y/n)? ");
}
}
如何循环整个程序以便用户可以反复使用它?
答案 0 :(得分:4)
有几种方法可以做到这一点,通常优先的方式可能是用while(true){
}
围绕整个主要功能,也许最后你提示用户询问他是否/她想再次这样做,如果他/她说不,你打电话给break
。