IF else statement for calculating max heart rate [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
need output to print
Your max heart rate is 191.5 beats per minute.
the number is will change due to the equation trying to be added.
so far this is the problem Im stuck on.
System.out.print("Your max heart rate is ");
if (gender == 1) { //calculate female heart rate
} else {//calculate male heart rate
}
System.out.println(maxHeartRate + "beats per minute.");
// Max heart rate for females:
209 - (0.7 * ageInYears)
// Max heart rate for males:
214 - (0.8 * ageInYears)
public static void main(String[] args) {
String name = "";
int ageInYears = 0;
int gender = 0;
double maxHeartRate = 0;
Scanner userInput = new Scanner(System.in);
//Prompt the user for thier name:
System.out.print("Enter your name: ");
name = userInput.nextLine();
//Prompt the user for their age:
System.out.print("Enter your age in years: ");
ageInYears = userInput.nextInt();
//Prompt the user to enter gender:
System.out.print("Enter your gender(1 for female and 0 for male): ");
gender = userInput.nextInt();
//convert age:
System.out.println("\tYour age in minutes is "
+ ageInYears * 525600 + "minutes.");
System.out.println("\tYour age in centuries is "
+ (double) ageInYears / 100 + "centuries.");
// display max heart rate
System.out.print("Your max heart rate is ");
}
}
this is the entire exercise that I have compiled so far. The If else is where Im getting hung up everytime my code is errors out for If else statement.

You have all the pieces, you just have to assembly them:
// Your previous code
// Calculate max heart rate
if (gender == 1)
maxHeartRate = 209 - (0.7 * ageInYears);
else
maxHeartRate = 214 - (0.8 * ageInYears);
// Then simply print it
System.out.print("Your max heart rate is " + maxHeartRate);

// Max heart rate for females:
if (gender ==1) {
maxHeartRate = 209 - (0.7 * ageInYears);
else {
// Max heart rate for males:
maxHeartRate = 214 - (0.8 * ageInYears);
}

if (gender == 1)
maxHeartRate = 209 - (0.7 * ageInYears);
else
maxHeartRate = 214 - (0.8 * ageInYears);
System.out.println(String.format("Your max heart rate is %s beats per minute.",maxHeartRate));

Related

Simple Retirement Fund Using Loops

I'm trying to make a program that calculates the amount of money someone would have after retiring at 67 and started saving at the age the entered.
The program should show how much the save each year at a rate of 2.5% and then the total after the entered amount of years.
So far I got the income to display for each year but the total seems to be 2.5% more than what it needs to be.
int age = 0;
double income;
double total = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("How old are you? ");
age = keyboard.nextInt();
System.out.print("What is your annual income? ");
income = keyboard.nextDouble();
System.out.print("Age(years) Income($)");
while(age < 68) {
System.out.print(age + " " + income);
income += (income * .025);
total += income;
age ++;
}
System.out.print("total($) " + total);
keyboard.close();
I've been using 62 as my age and 60000 as the amount per year. But when I print the total instead of getting ~383000 I'm getting ~392000.
you are not counting the last iteration 67-68 when you have <68 in the while loop
meaning when the age is 67 the loop doesn't run and therefore the last year's income is not calculated
should be while(age<=68)

I need help understanding the programming challenge called Shipping Charges

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

Lucky Sevens [Net Change Calculation] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
Whenever I execute the program the average change always is 0 and I do not understand why.
/*LuckySevens.java
Simulate the game of lucky sevens until all funds are depleted.
1) Rules:
roll two dice
if the sum equals 7, win $4, else lose $1
2) The inputs are:
the amount of money the user is prepared to lose
3) Computations:
use the random number generator to simulate rolling the dice
loop until the funds are depleted
count the number of rolls
keep track of the maximum amount
4) The outputs are:
the number of rolls it takes to deplete the funds
the maximum amount
the average net change after 100 rolls
*/
import java.util.Scanner;
import java.util.Random;
public class LuckySevens {
public static void main (String [] args) {
Scanner reader = new Scanner(System.in);
Random generator = new Random();
int die1, die2, // two dice
dollars, // initial number of dollars (input)
countAtMax, // count when the maximum is achieved
count, // number of rolls to reach depletion
maxDollars, // maximum amount held by the gambler
averageWin, // the average net change after 100 rolls
initialDollars; // initial amount of money user has
// Request the input
System.out.print("How many dollars do you have? ");
dollars = reader.nextInt();
// Initialize variables
maxDollars = dollars;
initialDollars = dollars;
countAtMax = 0;
count = 0;
// Loop until the money is gone
while (dollars > 0){
count++;
// Roll the dice.
die1 = generator.nextInt (6) + 1; // 1-6
die2 = generator.nextInt (6) + 1; // 1-6
// Calculate the winnings or losses
if (die1 + die2 == 7)
dollars += 4;
else
dollars -= 1;
// If this is a new maximum, remember it
if (dollars > maxDollars){
maxDollars = dollars;
countAtMax = count;
}
/* TODO:FIX BELOW STATEMENT
it always returns influx as 0 */
if (count == 100) {
averageWin = ((maxDollars - initialDollars) / 100);
System.out.println ("In the first 100 rolls there an average money influx of " + averageWin + " per roll.") ;
}
}
// Display the results
System.out.println
("You went broke after " + count + " rolls.\n" +
"You should have quit after " + countAtMax +
" rolls when you had $" + maxDollars + ".");
}
}
Make averageWin as double variable.
Change calculation of average win as below
averageWin = ((double)maxDollars - (double)initialDollars) / 100;
I tried the following to get the correct answer.
Changed type of averageWin
double averageWin;
And then calculation formula to
averageWin = (maxDollars - dollars) / 100.0;
This will preserve the precision and give you the correct answer.

Sea Level Java Project

I am trying to develop a program that calculates the sea level all the way up to the year 2100. Based on research I have found that the sea level will be 2.5 feet(which is 30 inches) - 6.5 feet(which is 78 inches). My program asks the user to enter which year they want to calculate how many inches the sea level has risen but I want the information to be random numbers per year between .3488 and .9069 inches because those are the averages per year that will make the sea level 2.5 feet and 6.5 feet. So my question is how can i generate random numbers for each year the user inputs so I can calculate how much the sea level has risen and then output that in the number of gallons.
/takes the input from the user of all the way up to which year
/they want to know the number of inches/ gallons the sea level has risen
/using reiman summs and a chart
/
/
/*******************************************************************************/
import java.util.Random;
import java.util.Scanner;
public class CalcProject
{
public static void main(String[] args)
{
//variables for the program
int year;
int numOfYears;
double leastRise = .3488; //y = .3488x , x is the inches of rainfall
double highRise = .9069; //y= .9069x , x is the inches of rainfall
double seaLevel1;
double seaLevel2;
//Describing what the program does to the user
System.out.println("This program caluclates the number of gallons / inches" +
"the sea level will rise based on your input." + "\n");
//Asking for the users year they want to calculate
System.out.println("Please enter which year you wish to know how much the water level will have risen since 2014");
Scanner scan = new Scanner(System.in);
year = scan.nextInt();
if(year <= 2014 || year > 2100)
{
do
{
System.out.println("Please enter a valid year between 2015 and 2100");
year = scan.nextInt();
} while((year <= 2014 || year > 2100));
}
//puts the year into a number of years from 2014
numOfYears = year-2014;
System.out.println("The number of years between now and the year you chose is: " + numOfYears);
//uses the least inches of sealevel
seaLevel1 = numOfYears * leastRise;
System.out.println("The total sealevel in inches is " + seaLevel1 + " for " + year);
//uses the most inches of seaLevel
seaLevel2 = numOfYears * highRise;
System.out.println("The total sealevel in inches is " + seaLevel2 + " for " + year);
//calculating radnom numbers for each year
Here is a simple way of handling the weird random numbers.
int numOfYears;
double leastRise = .3488;
double highRise = .9069;
double seaLevel3 = 0;
int i = 0;
while (i < numOfYears) {
double rand = Math.random();
if (rand >= leastRise && rand <= highRise) {
// rand is some sea level value representing a year
seaLevel3 += rand;
i++;
}
}
System.out.println("The total sealevel in inches is " + seaLevel3 + " for " + year);

issue with java while statement?

My code compiles correctly. When I run my program the output asks me to enter weight of package. If I enter a negative number the program asks me to enter weight again, but won't stop to let me enter another number.
I think the problem is in the "while" statement but I'm not sure.
Any help would be appreciated.
import java.util.Scanner;
public class dcrawford_Shipping
{
public static void main (String args[])
{
Scanner input = new Scanner(System.in);
int weight, distance, distancex;
double rate, price;
rate = 0.00;
System.out.print("Please enter package weight: ");
weight = input.nextInt();
while (weight <= 0 || weight >= 61)
{
System.out.print("Please enter package weight: ");
}
if (weight <= 10 && weight >= 1 )
{
rate = 5.01;
}
else if ( weight <= 20 && weight >= 11 )
{
rate = 7.02;
}
else if ( weight <= 30 && weight >= 21 )
{
rate = 9.03;
}
else if ( weight <= 40 && weight >= 31 )
{
rate = 11.04;
}
else if ( weight <= 60 && weight >= 41)
{
rate = 15.00;
}
System.out.print("Please enter distance: ");
distance = input.nextInt();
while ( distance <= 0 )
{
System.out.print("Please enter distance: ");
}
distancex = ( distance / 100 ) + 1;
price = ( distancex * rate );
System.out.printf("Your total shipping cost for %d miles is $%.2f\n", distance, price);
}
}
You need to ask the user to enter the weight again inside of the while loop.
while (weight <= 0 || weight >= 61) {
System.out.print("Please enter package weight: ");
weight = input.nextInt();
}
You could also use a do-while loop:
do {
System.out.print("Please enter package weight: ");
weight = input.nextInt();
} while (weight <= 0 || weight >= 61);
If you use the do-while loop, you can remove the first time you ask and the while loop. It's a slightly more compact way of doing it.

Categories

Resources