System.out.print("Your BMR is:");//BMR Calculation
if (gender == ('M')){
System.out.println(665+(6.23 * weight)+(12.7 * height)-(6.8 * age));
} else if (gender == ('F')) {
System.out.println(655+(4.35 * weight)+(4.7 * height)-(4.7 * age));
};
I can get the result from the if-loop above to produce the BMR. However, I can't seem to get the BMR result to be input into the bottom if-loop to produce the exercise calculation.
System.out.println("What is your exercise routine, on the scale of 0 to 4? 0 is lowest and 4 is highest ");//Exercise Calculation
int exercise = sc.nextInt();
if (exercise == 0) {
System.out.println(BMR * 1.2);
} else if (exercise == 1) {
System.out.println(BMR * 1.375);
} else if (exercise == 2) {
System.out.println(BMR * 1.55);
} else if (exercise == 3) {
System.out.println(BMR * 1.725);
}else if (exercise == 4) {
System.out.println(BMR * 1.9);
};
You should be storing the value of BMR into a local variable so that you can access it later on in your code.
Here's an example:
System.out.print("Your BMR is:");//BMR Calculation
double BMR = 0;
if (gender == ('M')){
BMR = 665+(6.23 * weight)+(12.7 * height)-(6.8 * age);
System.out.println(BMR);
} else if (gender == ('F')) {
BMR = 655+(4.35 * weight)+(4.7 * height)-(4.7 * age);
System.out.println(BMR);
};
And then you can use that BMR double variable later on.
Related
The first part of the code is working, the program takes inputs as height and weight as displays an output message. When I press n I can go to the second part and take inputs for height in inches and the weight in pounds. As soon as I take them the program outputs the resulting bmi is: Nan. and the variable bmi might not have been initialized. i want to convert the height and weight from inches and pounds to meters and then show the resulting bmi This is my code:
package uly14th;
import java.util.Scanner;
public class BmiCalculator {
private static float BMI2;
public static void main(String[] args) {
char Y;
char y;
char Q;
char n;
char N;
float heightInMeters, weightInKilograms, BMI;
float heightInInches, weightInpounds;
float heightInMeters2 = 0, weightInKilograms2 = 0;
Scanner input = new Scanner(System.in);
System.out.println("Please state whether you are going to use kilograms & meters or, inches & pounds");
System.out.println("If you want to use the former please press Y or if you want to use the latter please press N");
Q = input.next().charAt(0);
if(Q == 'y' || Q == 'Y' ) {
System.out.println("Please enter the height in meters: ");
heightInMeters = input.nextFloat();
System.out.println(" and weight in kilograms: ");
weightInKilograms = input.nextFloat();
BMI = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("The resulting BMI of the person is: " +BMI);
if (BMI < 18.5){
System.out.println("The personis underweight");
}
else if ((BMI<=25.0) && (BMI >= 18.5)){
System.out.println("The person is normal");
}
else if ((BMI >= 25.0) && (BMI <=30.0 )){
System.out.println("The personis obese");
}
else if(BMI>=30.0) {
System.out.println("The person is obese and should exercise");
}
}
if ((Q == 'n') || (Q == 'N')){
System.out.println("Please enter the height in inches: ");
heightInInches = input.nextFloat();
System.out.println("Please enter the weight in pounds: ");
weightInpounds = input.nextFloat();
heightInInches = 0.0254f * heightInMeters2;
weightInpounds = 0.453592f * weightInKilograms2;
BMI = weightInKilograms / (heightInMeters * heightInMeters);
System.out.println("The resulting BMI of the person is: " +BMI);
if (BMI < 18.5){
System.out.println("The personis underweight");
}
else if ((BMI<=25.0) && (BMI >= 18.5)){
System.out.println("The person is normal");
}
else if ((BMI >= 25.0) && (BMI <=30.0 )){
System.out.println("The personis obese");
}
else if(BMI>=30.0) {
System.out.println("The person is obese and should exercise");
}
}
else {
System.out.println("Please make sure you run the program again and then only use the characters n,N,y,Y. ");
}
}
}
This is the error message: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - variable BMI might not have been initialized
at uly14th.BmiCalculator.main(BmiCalculator.java:82)
At the row (line 82)
BMI = weightInKilograms / (heightInMeters * heightInMeters);
you use the variables weightInKilograms and heightInMeters which has never been initialized, hence your error.
I suspect you want to do the conversion to meters and kilograms from inches and pounds before calculating the BMI. You can do this by simply putting the following lines before the BMI calculation
heightInMeters = 0.0254f * heightInInches;
weightInKilograms = 0.453592f * weightInPounds;
BMI = weightInKilograms / (heightInMeters * heightInMeters); //Now you can calculate the BMI using your variables since they have been assigned a value and is therefore initialized
heightInMeters = 0.0254f * heightInInches ;
weightInKilograms = 0.453592f * weightInpounds ;
BMI = weightInKilograms / (heightInMeters * heightInMeters);
should do the trick (... in case the constants are correct)
So right now everything works fine so long as the volume of the item(p1v and p2v) is greater than 1 but I can't figure out a way for the console to print the base price of $3 if the volume is less than 1. For reference the cost of an item starts at $3 for the first unit of volume or part thereof then increases by $1 for each additional unit.
public void calculateCost() {
NumberFormat currency = NumberFormat.getCurrencyInstance();
if (p1v < 1) {
double cost = 3;
System.out.printf("%nPackage 1 will cost " + currency.format(cost) + " to ship.");
}
if (p2v < 1) {
double cost2 = 3;
System.out.printf("%nPackage 1 will cost " + currency.format(cost2) + " to ship.");
}
if (p1v < 1 && p2v < 1) {
double cost = 3 + p1v - 1;
String message = "Package 1 will cost " + currency.format(cost) + " to ship.";
System.out.printf("%n" + message);
double cost2 = 3 + p2v - 1;
String message2 = "Package 2 will cost " + currency.format(cost2) + " to ship.";
System.out.printf("%n" + message2);
if (cost >= 5 * cost2) {
System.out.printf("%nThe first package is " + cost / cost2 + "times the cost of the second package.");
} else if (cost2 >= 5 * cost) {
System.out.printf("%nThe second package is " + cost2 / cost + "times the cost of the first package.");
} else if (cost == cost2) {
System.out.printf("%nThe first package is the same cost as the second package.");
} else if (cost < 5 * cost2 && cost >= 4 * cost2) {
System.out.printf("%nThe first package is quadruple the cost of the second package.");
} else if (cost2 < 5 * cost && cost2 >= 4 * cost) {
System.out.printf("%nThe second package is quadruple more than the cost of the first package.");
} else if (cost < 4 * cost2 && cost >= 3 * cost2) {
System.out.printf("%nThe first package is triple the cost of the first package.");
} else if (cost2 < 4 * cost && cost2 >= 3 * cost) {
System.out.printf("%nThe second package is triple the cost of the second package.");
} else if (cost < 3 * cost2 && cost >= 2 * cost2) {
System.out.printf("%nThe first package is twice the cost of the first package.");
} else if (cost2 < 3 * cost && cost2 >= 2 * cost) {
System.out.printf("%nThe second package is twice the cost of the second package.");
} else if (cost < 2 * cost2) {
System.out.printf("%nThe second package is slightly more than the cost of the first package.");
} else if (cost2 < 2 * cost) {
System.out.printf("%nThe first package is slightly more than the cost of the second package.");
}
}
}
The question says The Fast Freight Shipping Company charges the following rates:
Weight of Package Rate per 500 Miles Shipped
2 pounds or less $1.10
Over 2 pounds but not more than 6 pounds $2.20
Over 6 pounds but not more than 10 pounds $3.70
Over 10 pounds $3.80
The shipping charge per 500 miles are not prorated. For example, if a 2-pound package is shipped 550 miles, the charges would be $2.20. Write a program that asks the user to enter the weight of a package and then displays the shipping charges.
My problem is that I keep receiving two different answers everytime I put in a weight and distance. For example when I enter the weight as 2 pounds and the distance as 500 miles I get the answers $0.0 and $3.8 which are both incorrect answers. It looks like some weights that I enter are correct answers and others I enter give me incorrect answers. Heres my program:
//import java utilities for scanner class
import java.util.Scanner;
public class ShippingCharge
{
public static void main (String[] args)
{
//Declare and initialize variable to hold the entered weight.
int weight = 0;
//Declare and initialize variable to hold the entered distance.
double distance = 0.0;
//This variable will hold the calculated rate.
double rate;
//This will decide if the shipping charge will advance up one level.
int distanceMultiplier = (int)distance / 500;
//This will hold the increments of the shipping charge.
int distanceRemainder;
//Create a Scanner object for the input.
Scanner input = new Scanner(System.in);
//Get the weight of the package.
System.out.println("What is the weight of the package (in pounds)?");
weight = input.nextInt();
//Get the shipping distance of the package.
System.out.println("What is the shipping distance (in miles)?");
distance = input.nextDouble();
distanceRemainder = (int)distance % 500;
if (distanceRemainder == 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.70));
}
else
{
System.out.println("Total Shipping Cost is: $" + (distanceMultiplier * 3.80));
}
if (distanceRemainder != 0)
{
if (weight <= 2)
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.70);
}
else
{
System.out.println("Total Shipping Cost is: $" +(distanceMultiplier + 1) * 3.80);
}
//end program
System.exit(0);
}//end main
}//end class
This will work for you
public static void main(String[] args) {
int weight = 0;
double distance = 0.0 , distanceExtra ;
Scanner in = new Scanner(System.in);
System.out.println("Weight ? ");
weight = in.nextInt();
System.out.println("Distance ? ");
distance = in.nextDouble();
distanceExtra = distance / 500;
distanceExtra = Math.ceil(distanceExtra);
if (weight <= 2) {
System.out.printf("charge is :" , (distanceExtra * 1.10));
}
else if (weight > 2 && weight <= 6)
{
System.out.printf("charge is :" , (distanceExtra * 2.20));
}
else if (weight > 6 && weight <= 10)
{
System.out.printf("charge is :" , (distanceExtra * 3.70));
}
else if (weight > 10)
{
System.out.printf("charge is :" , (distanceExtra * 4.80));
}
}
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
int weight = 0;
double distance = 0.0 ;
Scanner keyboard =new Scanner(System.in);
System.out.println("Enter the Distance");
distance = keyboard.nextDouble();
System.out.println("Enter the Weight");
weight = keyboard.nextInt();
if (weight <= 2) {
System.out.println("charge is : " + "$"+1.10);
}
else if (weight > 2 && weight <= 6)
{
System.out.println("charge is : " + "$"+2.20);
}
else if (weight > 6 && weight <= 10)
{
System.out.println("charge is : " + "$"+3.70);
}
else if (weight > 10)
{
System.out.println("charge is :" + "$"+4.80);
}
}
}
I get an error saying "The left-hand side of an assignment must be a variable" where is says else (itemNumber >=15)
import java.util.Scanner;
public class Ch3Asg
{
public static void main(String[] args)
{
// Variables
Scanner input = new Scanner(System.in);
int itemNumber = 0;
double shippingCost = 0;
// Items Purchased
System.out.println("How many items did you purchase? ");
itemNumber = Integer.parseInt(input.nextLine());
// One Item Purchased
if ( itemNumber == 1 )
{
shippingCost = 2.99;
}
// 2-5 Items Purchased
else if ( itemNumber >= 2 && itemNumber <= 5 );
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1);
}
// 5-15 Items Purchased
if ( itemNumber > 5 && itemNumber < 15)
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1) + 1.49 * (itemNumber - 5);
}
// More Than 15 Items Purchased
else ( itemNumber >= 15 )
{
shippingCost = 2.99 + 1.99 * (itemNumber - 1) + 1.49 * (itemNumber - 5)
+ .99 * (itemNumber - 14);
}
// Display Cost
System.out.printf("Shipping Cost is: $%.2f", shippingCost);
}
}
else (boolean statement) is meaningless. I think that you forgot the if:
else ( itemNumber >= 15 )
should be
else if ( itemNumber >= 15 )
Or else if it represents the last and default option, it could be simply:
else {
//..
}
else construct does not take an expression, so
else ( itemNumber >= 15 )
is syntactically wrong.
You need to use else if instead
else if ( itemNumber >= 15 )
I have done a google around but can't get my head around what's wrong with my code here.
I'm getting an Illegal start of expression error in the if statement at the first 'Else if'
I'm pretty new to Java but trying to quickly learn as its a requirement of my computing course I'm hoping to do.
Any help greatly appreciated.
import java.util.Scanner;
public class net_salary{
public static void main(String[]args){
Scanner in = new Scanner(System.in);
int income;
int incometax;
int afterincometax;
int nationalinsurance;
int afterni;
int pension;
income = in.nextInt();
if(income = 0) {
incometax = 0;
} else if (income >=9000 & <=21000) {
incometax = income * 0.15;
} else if (income >=21000 & <=31000) {
incometax = income * 0.2;
} else if (income >=31000 & <=50000) {
incometax = income * 0.225
You need else if (income >=9000 && income <=21000) instead of else if (income >=9000 & <=21000).
Similarly for the other else if expressions.
You have to make two separate comparisons to income. You can't say "if income is greater than or equal to 9000 or less than or equal to 21000". You must say "if income is greater than 9000 or income is less than or equal to 21000".
& is a bitwise AND. && is logical AND.
As stated by others, income = 0 should read income == 0.
You need && not &, and repeat the variable 'income' :
else if (income >=9000 && income <=21000)
You are also using = where you probably mean ==
if(income == 0)
= assigns a value, == tests whether two values are equal.
Your if/else if block expression always should results as boolean (either true/false). In Java, && represents AND, & is bitwise operator which manipulates bits.
if(income == 0)
{
incometax = 0;
}
**else if (income >=9000 && income <=21000)**
{
incometax = income * 0.15;
}
else if (income >=21000 && income <=31000)
{
incometax = income * 0.2;
}
else if (income >=31000 && income <=50000)
{
incometax = income * 0.225
if(income = 0)
should be
if(income == 0)
and
else if (income >=9000 & <=21000)
(and it's followers) should be
else if (income >=9000 && income <=21000)
and looks like you you need to make some of your variables double instead of int.
Check the usage of "=", "==", "&", "&&" operators in Java.
public class net_salary{
public static void main(String[]args){
int income, ncometax, afterincometax, nationalinsurance, afterni, pension;
Scanner in = new Scanner(System.in);
income = in.nextInt();
if(income == 0) {
incometax = 0;
} else if (income >= 9000 && income <= 21000) {
incometax = income * 0.15;
} else if (income >=21000 && income <= 31000) {
incometax = income * 0.2;
} else if (income >=31000 && income <= 50000) {
}
}
}