Im creating a bmi caculator and im using the height and weight formula. I keep getting an erorr that says nan obese at the very end and i dont know what i'm missing or what i need to edit. Any help would be appreciated.
Output error:
Enter Weight in pounds: 150
Enter height (ft.):
6
Enter height (in.):
7
Your BMI is: NaN
Obese
BMI formula:
BMI = (703 * weightInPounds) / heightInInches^2
Code:
import java.util.Scanner;
public class Bmi_Calc {
public static void main(String[] args) {
// TODO Auto-generated method st
Scanner input = new Scanner(System.in);
//double weight;
double weightInPounds;
int feet;
int inches;
//int weightInPounds;
int heightInInches;
double height;
System.out.print("Enter Weight in pounds: ");
double weight = input.nextDouble();
System.out.println("Enter height (ft.): ");
feet = input.nextInt();
System.out.println("Enter height (in.): ");
inches = input.nextInt();
weightInPounds = 0;
heightInInches = 0;
double bmi = (703 * weightInPounds) / Math.pow(heightInInches, 2.0);
double heightMeters = ((feet * 12) + inches) * .0254;
System.out.println("Your BMI is: " + bmi);
if (bmi < 18.5) {
System.out.println("Underweight.");
}
if ((bmi >= 18.5) && (bmi < 24.9)) {
System.out.println("Normal weight");
}
if ((bmi >= 25) && (bmi < 29.9)) {
System.out.println("Overwight");
}
else {
System.out.println("Obese");
}
input.close();
}
}
Check your bmi calculation numbers. That will tell you why you are getting NAN.
weightInPounds = 0;
heightInInches = 0;
double bmi = (703 * weightInPounds) / Math.pow(heightInInches, 2.0);
In above weightInPounds and heightInInches are zero so bmi = 0/0 which is causing NaN
Thanks
Related
I am very new to Java and my assignment was to make a tax calculator. Everything seems to be working fine but when I print at the end it always prints 0 for federaltaxes. I make the double at the start and then try to reassign it depending on the situation through the else if statement.
//2353161 Tomas Pospisil
import java.util.Scanner;
public class PaymentCalculator {
public static void main(String[] args){
//Create Scanner for user input
Scanner input = new Scanner(System.in);
//Ask the user for Gross Pay
System.out.print("Please Enter Gross Pay: ");
double GrossPay = input.nextInt();
//Establish the integer for Number of Exemptions
int NoE = 0;
//Establish the integer for the value of exemption
double exemptionvalue = 0;
double federaltaxes = 0;
//Ask the user if they are married
System.out.print("Are you married: ");
String marriedstatus = input.next();
//If yes make the number of Exemptions equal the user input
if (marriedstatus.equals("yes")) {
System.out.print("Please enter Number of Exemptions: ");
NoE = input.nextInt();
}
//If not make the number of exemptions equal 1
else if(marriedstatus.equals("no")) {
NoE = 1;
}
//If NoE is greater than 4 make it 5 for later use
if (NoE >4) {
NoE = 5;
}
// Depending on the number of exemptions the program assigns it a value for later use
switch (NoE) {
case 1:
exemptionvalue = 1000;
break;
case 2:
exemptionvalue = 1800;
break;
case 3:
exemptionvalue = 2400;
break;
case 4:
exemptionvalue = 3600;
break;
case (5):
exemptionvalue = 4000;
break;
}
//Calculate adjusted wages
double adjustedwages = GrossPay - exemptionvalue;
// Figure out the tax percentage
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
}
else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15 ;
}
else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20 ;
}
//Program calculates the FICA
double FICA = (GrossPay * .065);
//Program calculates Medicare
double Medicare = (GrossPay * .014);
//Program calculates the federal taxes
//Program Calculates Net Pay
double NetPay = GrossPay - (FICA + Medicare + federaltaxes);
//Program prints the results
System.out.println(" Payroll Taxes");
System.out.println("Gross.....$ " + GrossPay);
System.out.println("Exemptions: " + NoE);
System.out.println("Federal Taxes.....$ " + federaltaxes);
System.out.println(" FICA.....$ " + FICA);
System.out.println(" Medicare.....$ " + Medicare);
System.out.println("-----------------------------");
System.out.println("Net Pay.....$ " + NetPay);
}
}
This is the block that is causing you trouble.
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
}
Any GrossPay amount above 10000 is going to pass the condition and be multiplied by 0.
You have a wrong condition in the if where you evaluate federal taxes, it should be:
if (GrossPay < 10000) {
federaltaxes = GrossPay * 0;
}
else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15 ;
}
else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20 ;
}
your code will never evaluate the else condition
It is because your condition logic is wrong
double federaltaxes = 0;
// Figure out the tax percentage
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
} else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15;
} else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20;
}
Your first condition will always be true if the Grosspay is >= 10000 so federaltaxes here is 0, then the rest of your if else will not be evaluated because your first condition will always evaluate first. Also you dont have a separate else statement for the rest of the scenarios
I want my main method to contain input values for a person’s weight and height and then for it to call for the bodyMassIndex method to get the health opinion (i.e. normal weight) which should then be output. Wondering if anyone could help me?
package bodymassindex;
import java.util.Scanner;
public class BodyMassIndex {
public static void main(String[] args) {
double m;
double kg;
double bmi;
Scanner input = new Scanner(System.in);
System.out.println("Enter weight (KG): ");
kg = input.nextDouble();
System.out.println("Enter height (M): ");
m = input.nextDouble();
bmi = kg / (m*m);
System.out.println("Your BMI is " + bmi);
if (bmi < 16)
System.out.println("Seriously underweight");
else if (bmi >= 16 && bmi < 18)
System.out.println("Underweight");
else if (bmi >= 18 && bmi < 24)
System.out.println("Normal weight");
else if (bmi >= 24 && bmi < 29)
System.out.println("Overweight");
else if (bmi >= 29 && bmi < 35)
System.out.println("Seriously overweight");
else if (bmi >= 35 )
System.out.println("Obese");
}
}
This breaks it down into its own method, and create a method which passes in some test parameters. Also includes functionality to take in user input still.
package bodymassindex;
import java.util.Scanner;
public class BodyMassIndex {
public static void main(String[] args) {
double m;
double kg;
Scanner input = new Scanner(System.in);
System.out.println("Enter weight (KG): ");
kg = input.nextDouble();
System.out.println("Enter height (M): ");
m = input.nextDouble();
bmi(kg, m);
// bmiTest(); /* Uncomment to run some "test" values.*/
}
private static void bmiTest() {
bmi(100, 2);
bmi(50, 1.3);
bmi(60, 1.8);
}
private static void bmi(double kg, double m) {
double bmi = kg / (m*m);
System.out.println("Your BMI is " + bmi);
if (bmi < 16)
System.out.println("Seriously underweight");
else if (bmi >= 16 && bmi < 18)
System.out.println("Underweight");
else if (bmi >= 18 && bmi < 24)
System.out.println("Normal weight");
else if (bmi >= 24 && bmi < 29)
System.out.println("Overweight");
else if (bmi >= 29 && bmi < 35)
System.out.println("Seriously overweight");
else if (bmi >= 35 )
System.out.println("Obese");
}
}
The goal of this assignment is to read two numbers from the user, compute the distance between those numbers, then continue to ask the user for two numbers until they are equal, each time competing the distance, and the goal is to find the minimum distance between two numbers. We have to create a method that finds the minimum distance, and once the two numbers are equal, to print that minimum distance (only once). And the distance between the numbers that are equal does not count as a minimum distance. How do I do this? Thank you.
This is the code that I currently have.
double myMin = Double.MAX_VALUE;
while ( !(num1==num2)) {
double dist;
pairsMin(dist,myMin,num1,num2);
// re-ask for user input of num1 and num2
Min1 = pairsMin(dist,myMin,num1,num2);
}
System.out.print("\nThe minimum distance is: " + Min1 + "\n");
} // end of main method
public static double pairsMin( double dist, double myMin, double num1,double num2){
dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
} // end of pairsMin method
You have to make minor changes to your logic. Try below
public static int pairsMin(int min, int num1, int num2) {
int dist = 0;
dist = Math.abs(num1 - num2);
if (dist != 0 && dist < min) {
min = dist;
}
return min;
}
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int num1;
int num2;
int mymin = Integer.MAX_VALUE;
do {
System.out.print("Enter number 1: ");
num1 = kb.nextInt();
System.out.print("Enter number 2: ");
num2 = kb.nextInt();
mymin = pairsMin(mymin, num1, num2);
} while (!(num1 == num2));
System.out.print("\nThe minimum distance is: " + mymin + "\n");
} // end of main method
This is what I know have, using your suggestions, and it is working. I didn't think that my if statement in my while loop would execute because I thought once num1==num2, that the while loop would end, but it worked. Thank you!
import java.util.Scanner;
public class hw5_pairs {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number 1: ");
double num1 = in.nextDouble();
System.out.print("Enter number 2: ");
double num2 = in.nextDouble();
double Min1;
double myMin = Double.MAX_VALUE;
double dist = Math.abs(num1-num2);
while ( !(num1==num2)) {
pairsMin(dist, num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
Min1 = pairsMin(dist,num1, num2, myMin);
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n");
}
}
}
public static double pairsMin( double dist, double num1, double num2, double myMin){
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}
My new problem is that myMin is equalling the last distance before the numbers are equal instead of the actual minimum. e.g. say the first two numbers I enter are 1 and 2, and the next are 1 and 3, and then 1 and 1. It is saying my minimum is 2.0.
This is what I'm supposed to get for the assignment.
Enter number 1: 9
Enter number 2: 1
Enter number 1: 7
Enter number 2: 2
Enter number 1: 4
Enter number 2: 4
The minimum distance is: 5.0.
Enter number 1: 20
Enter number 2: 3
Enter number 1: 23
Enter number 2: 23
5.0 + 17.0 = 22.0
MY CODE:
double myMin = Double.MAX_VALUE;
double Min1,Min2;
while ( !(num1==num2) ) {
pairsMin( num1, num2, myMin);
Min1 = pairsMin( num1, num2, myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if (num1==num2) {
System.out.print("\nThe minimum distance is: " + Min1 + "\n\n");
myMin = Double.MAX_VALUE;
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
while ( !(num1==num2)) {
pairsMin( num1, num2, myMin);
Min2 = pairsMin(num1,num2,myMin);
System.out.print("Enter number 1: ");
num1 = in.nextDouble();
System.out.print("Enter number 2: ");
num2 = in.nextDouble();
if(num1==num2) {
double totMin = Min1+Min2;
System.out.print("\n" + Min1 + " + " + Min2 + " = " + totMin + "\n");
}
}
}
} // end while loop
} // end main method
public static double pairsMin( double num1, double num2, double myMin){
double dist = Math.abs(num1-num2);
if ( dist<myMin) { // if dist is smaller than the minimum, then dist will be the new minimum
myMin = dist;
}
return myMin;
}
}
When the user enters a width that is less than or equal to 0, the program should end, and not prompt the user for a length, or output an area or perimeter. How do I do this WITHOUT using System.exit(0)?!?
public static void main(String[] args) {
Scanner k = new Scanner (System.in);
System.out.print("Please Enter a Width: ");
double width = k.nextDouble();
System.out.print("Please Enter a Length: ");
double length = k.nextDouble();
double area = width * length;
System.out.println("The Area is: " + area);
double perimeter = 2 * (width + length);
System.out.println("The Perimeter is: " + perimeter);
if (width <=0)
System.out.println("The Width Must Be Greater Than 0");
else if (length <=0)
System.out.println("The Length Must Be Greater Than 0");
if (width == length)
System.out.println("The Shape is a Square");
else if (width != length)
System.out.println("The Shape is a Rectangle");
You can always jump out of a method with return
In your case:
...
System.out.print("Please Enter a Width: ");
double width = k.nextDouble();
if (width <= 0)
return;
...
If the width is less than zero, the program will exit and not prompt the user for the length. I've expanded that logic into the length prompt, such that if the length is less than 0, the program stops as well.
Working off of brso05's logic, I've come up with the below suggestion:
Scanner k = new Scanner (System.in);
System.out.print("Please Enter a Width: ");
double width = k.nextDouble();
if (width <=0){
System.out.println("The Width Must Be Greater Than 0");
}
else{
System.out.print("Please Enter a Length: ");
double length = k.nextDouble();
if (length <=0){
System.out.println("The Length Must Be Greater Than 0");
}
else{
double area = width * length;
System.out.println("The Area is: " + area);
...
}
}
Change your conditional statements so the rest of the code is executed only if the width is greater than 0:
Scanner k = new Scanner (System.in);
System.out.print("Please Enter a Width: ");
double width = k.nextDouble();
if (width <=0)
{
System.out.println("The Width Must Be Greater Than 0");
}
else
{
System.out.print("Please Enter a Length: ");
double length = k.nextDouble();
double area = width * length;
System.out.println("The Area is: " + area);
double perimeter = 2 * (width + length);
System.out.println("The Perimeter is: " + perimeter);
if (length <=0)
System.out.println("The Length Must Be Greater Than 0");
if (width == length)
System.out.println("The Shape is a Square");
else if (width != length)
System.out.println("The Shape is a Rectangle");
}
So I have Been working on a Program and it has been awhile since I have last used java. I was wondering how to get my program to accept decimals. I have tried looking it up but I couldn't find anything helpful and anything I really understood.Below is what I have done so far...
package test;
import java.util.Scanner;
/**
*
* #author Thao
*/
public class Test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
double cash; // double makes it allow decimals
int hundred;
int twenty;
int ten;
int toonies ;
int lonies;
int quarters;
int dimes;
int nickels;
int pennies;
int money;
int change;
System.out.println();// blank line
System.out.print("Hello ");
System.out.println();
System.out.println("Please enter a number with no decimal places ");
cash = input.nextInt(); // read first integer
System.out.println("you have Entered "+cash);
hundred = 100;
change = (int)hundred - (int)cash;
System.out.println("The change from $100.00 is:" + (double)change);
change = 100 * change; // multiply cash by 100
money = (int)change; //cash is now a int
twenty = money / 2000;
money = money - (twenty * 2000);
toonies = money / 200; // money is multiplied by 100 than / by 200
money = money - (toonies * 200); // ex. money = 1586 - (7.93 * 200 )
lonies = money / 100 ;
money = money - (lonies * 100);
quarters = money / 25;
money = money - (quarters * 25);
dimes = money / 10;
money = money - (dimes * 10);
nickels = money / 5;
money = money - (nickels * 5);
pennies = money / 1;
money = money - (pennies * 1);
if(twenty>0){
System.out.println();
System.out.print("You will have this many Twenties ");
System.out.print(twenty + ".");
}
else{
}
if(toonies>0){
System.out.println();
System.out.print("You will have this many Toonies ");
System.out.print(toonies + ".");
System.out.println();
}
else{
}
if(lonies>0){
System.out.print(" You will have this many Lonies ");
System.out.print(lonies + ".");
System.out.println();
}
else{
}
if(quarters>0){
System.out.print(" You will have this many quarters ");
System.out.print(quarters + ".");
System.out.println();
}
else{
}
if(dimes>0){
System.out.print(" You will have this many dimes ");
System.out.print(dimes + ".");
System.out.println();
}
else{
}
if(dimes>0){
System.out.print(" You will have this many nickels ");
System.out.print(nickels);
System.out.println();
}
else{
}
if(pennies>0){
System.out.print(" You will have this many pennies ");
System.out.print(pennies);
System.out.println();
}
else{
}
System.out.println();
System.out.println("Thank you for using my app ");
}
}
You can use double or float for the variable's data type. To read a double from your input you would do:
double cash = input.nextDouble();
or
float cash = input.nextFloat();