hi, why my code when i try to calculate BMI every time the answer equal 0.0 and can't get the right answer? package a; import java.util.*; public class a { public static void main(String []args){ Scanner sc=new Scanner(System.in); label: System.out.println("Enter the number of cities"); int noOfcities,nPerson; noOfcities=sc.nextInt(); int under=0,obese=0,normal=0,over=0,c_under=0,c_obese=0,c_normal=0,c_over=0; double p_under,p_over,p_obese,p_normal; double max_normal=0.0,max_obese=0.0,min_under=100.0; String name,b_city="None",w_city="None",u_city="None",cat; int mass,height; while ( noOfcities<6){ System.out.println("erro"); System.out.println("Enter the number of cities"); noOfcities=sc.nextInt(); }int i=0; while (i=10) for(int j=0;j=18.5 && bmi<25) { cat="normal"; c_normal++; } else if(bmi>=25 && bmi<30) { cat="overweight"; c_over++; } else { cat="obese"; c_obese++; } System.out.println("The person has a BMI of "+bmi+" and belongs to category "+cat); } under+=c_under; over+=c_over; normal+=c_normal; obese+=c_obese; p_under=(under/nPerson)*100; p_normal=(normal/nPerson)*100; p_over=(over/nPerson)*100; p_obese=(obese/nPerson)*100; System.out.println("For the city "+name+" category wise breakup is:"); System.out.println("Number of people who are Underweight: "+under+ ", Percentage is: "+p_under); System.out.println("Number of people who are Normal: "+normal+ ", Percentage is: "+p_normal); System.out.println("Number of people who are Overweight: "+over+ ", Percentage is: "+p_over); System.out.println("Number of people who are Obese: "+obese+ ", Percentage is: "+p_obese); if(p_normal>max_normal) { max_normal=p_normal; b_city=name; } if(p_obese>max_obese) { max_obese=p_obese; w_city=name; } if(p_under
hi, why my code when i try to calculate BMI every time the answer equal 0.0 and can't get the right answer?
package a;
import java.util.*;
public class a {
public static void main(String []args){
Scanner sc=new Scanner(System.in);
label:
System.out.println("Enter the number of cities");
int noOfcities,nPerson;
noOfcities=sc.nextInt();
int under=0,obese=0,normal=0,over=0,c_under=0,c_obese=0,c_normal=0,c_over=0;
double p_under,p_over,p_obese,p_normal;
double max_normal=0.0,max_obese=0.0,min_under=100.0;
String name,b_city="None",w_city="None",u_city="None",cat;
int mass,height;
while ( noOfcities<6){
System.out.println("erro");
System.out.println("Enter the number of cities");
noOfcities=sc.nextInt();
}int i=0;
while (i<noOfcities){
i++;
System.out.println("Enter the name of city");
name=sc.next();
System.out.println("Enter the number of persons");
nPerson=sc.nextInt();
while (nPerson<10)
{System.out.println("eror");
System.out.println("Enter the number of persons");
nPerson=sc.nextInt();
}
c_under=0;c_obese=0;c_normal=0;c_over=0;
if(nPerson>=10)
for(int j=0;j<nPerson;j++)
{
System.out.println("Enter the mass and height");
mass=sc.nextInt();
height=sc.nextInt();
while (mass<1||height<1){
System.out.println("error");
System.out.println("Enter the mass and height");
mass=sc.nextInt();
height=sc.nextInt();
}
double bmi = mass / ( height * height) ;
if(bmi<18.5)
{
cat="underweight";
c_under++;
}
else if(bmi>=18.5 && bmi<25)
{
cat="normal";
c_normal++;
}
else if(bmi>=25 && bmi<30)
{
cat="overweight";
c_over++;
}
else
{
cat="obese";
c_obese++;
}
System.out.println("The person has a BMI of "+bmi+" and belongs to category "+cat);
}
under+=c_under;
over+=c_over;
normal+=c_normal;
obese+=c_obese;
p_under=(under/nPerson)*100;
p_normal=(normal/nPerson)*100;
p_over=(over/nPerson)*100;
p_obese=(obese/nPerson)*100;
System.out.println("For the city "+name+" category wise breakup is:");
System.out.println("Number of people who are Underweight: "+under+ ", Percentage is: "+p_under);
System.out.println("Number of people who are Normal: "+normal+ ", Percentage is: "+p_normal);
System.out.println("Number of people who are Overweight: "+over+ ", Percentage is: "+p_over);
System.out.println("Number of people who are Obese: "+obese+ ", Percentage is: "+p_obese);
if(p_normal>max_normal)
{
max_normal=p_normal;
b_city=name;
}
if(p_obese>max_obese)
{
max_obese=p_obese;
w_city=name;
}
if(p_under<min_under)
{
min_under=p_under;
u_city=name;
}
}
System.out.println("tttt");
System.out.println("Number of Cities are: "+noOfcities);
System.out.println("Best city is "+b_city);
System.out.println("Worst city is "+w_city);
System.out.println("The city which has the minimum percent of people in underweight category is "+u_city);
}
Step by step
Solved in 3 steps with 1 images