Ask coding questions

← Back to all posts
Mini Project 1
Steven204322 (2)

How do I get my user to actually be able to input what exercise or treat and actually input it like for example it says that she can have a treat and the user then puts in Snickers, and then the program goes ahead and runs.

Commentshotnewtop
RamonNeres (0)

You created the Scanner kb and used it to get the number calories eaten and after this, you used the same kb to get the treat/exercise.
You can try to create 2 separate Scanners. One of them to get numbers and the other to get text.

Steven204322 (2)

@RamonNeres would you mind giving like an example? Sorry I am a beginner.

RamonNeres (0)

@Steven204322 No problem.
At line 7 you created you Scanner kb and you use it to get the calories at line 10, to get the treat at line 20 and to get the exercise at line 45.
So, you can create another Scanner (name it kb2, for example), and change the lines 20 and 45 to use your new kb2 scanner.

Steven204322 (2)

@RamonNeres alright it is still not working :(

Steven204322 (2)

@RamonNeres Alright so now I got the scanner to work and I can input it, but it does not take the input calories and add more calories if she ate a treat or subtract calories to take away the burned calories could you please tell me how to do that?

Steven204322 (2)

@RamonNeres import java.util.*;

//Fitness Fanatic: Melanie Muscle

public class Main{
public static void main(String[] args){
Scanner kb = new Scanner(System.in);
Scanner sc = new Scanner(System.in);
System.out.println("Hello Melanie! Your maximum number of calories you can consume is 1500.");
System.out.println("Please enter in the amount of calories you have eaten please.");
double caloriesConsumed = kb.nextDouble();
if(caloriesConsumed < 1500){

  double snickersCalories = 488;
  double tacoCalories = 156;
  double burritoCalories = 434;
  double skittlesCalories = 231;
  double chocolateCalories = 486;
  System.out.println("What treat would you like to have out of: Snickers, Taco, Burrito, Skittles, or Chocolate?");
  String treat = sc.nextLine();
  double newTotalCalories;
  if(treat.equals("Snickers")){
    newTotalCalories = (snickersCalories + caloriesConsumed);
  }
  if(treat.equals("Taco")){
    newTotalCalories = (tacoCalories + caloriesConsumed);
  }
  if(treat.equals("Burrito")){
    newTotalCalories = (burritoCalories + caloriesConsumed);
  }
  if(treat.equals("Skittles")){
    newTotalCalories = (skittlesCalories + caloriesConsumed);
  }
  if(treat.equals("Chocolate")){
    newTotalCalories = (chocolateCalories + caloriesConsumed);
  }
}
if(caloriesConsumed > 1500){
  double pushUpsCalories = 430;
  double suicidesCalories = 300 ;
  double squatsCalories = 420;
  double runningCalories = 480;
  double bikingCalories = 184;
  System.out.println("You have reached or exceeded your maximum amount of calories! Please select an exercise out of your following choices: Push Ups, Suicides, Squats, Running, or Biking.");
    String choiceOfExercise = sc.nextLine();
    double newTotalCalories = (caloriesConsumed);
      
      if(choiceOfExercise.equals("Push Ups")){
        newTotalCalories = (caloriesConsumed - pushUpsCalories);
      }
      if(choiceOfExercise.equals("Suicides")){
        newTotalCalories = (caloriesConsumed - suicidesCalories);
      }
      if(choiceOfExercise.equals("Squats")){
        newTotalCalories = (caloriesConsumed - squatsCalories);
      }
      if(choiceOfExercise.equals("Running")){
        newTotalCalories = (caloriesConsumed - runningCalories);
      }
      if(choiceOfExercise.equals("Biking")){
        newTotalCalories = (caloriesConsumed - bikingCalories);
      }
   if(newTotalCalories >= 1500){
      System.out.println("Good Job! Have a treat!");
  }
 }

}
}

RamonNeres (0)

@Steven204322 I think the problem is that you are not printing it. When you choose a treat or an exercise, the program add/subtract the calories, but there is no command to print it.

RamonNeres (0)

@Steven204322 One more thing, at line 63 I think the >= should be a <=

Steven204322 (2)

@RamonNeres ohhhh okay let me try that

Steven204322 (2)

@RamonNeres sorry I know this may sound dumb but what would be the print statement?

Steven204322 (2)

@RamonNeres Nevermind I figured it out but, the only thing I have left now is doing the loop so that if she has her snack and it exceeds the limit then she has to exercise. How do I do a loop, because my teacher never taught that yet.

RamonNeres (0)

@Steven204322 There are two ways to do a loop in java.
The 'while', it would repeat a block of code while the condition is true.

while(condition) {
code
}

The other is the 'for'. The same thing, but it let you create a counter variable to repeat a number of times.

for(int i = 0 ; i < 10 ; i++) { // This will repeat 10 times starting from i = 0 to i = 9.
code;
}

I think the loop you should use is the while loop.

Steven204322 (2)

@RamonNeres Could you please explain how to fully use the while loop and why I should use the while loop?

RamonNeres (0)

@Steven204322 I think you want to repeat the code while the calories consumed are different than 1500, so you can write:\
while(calories != 1500) {
if(calories < 1500) {
choose treat
}
if(calories > 1500) {
choose exercise
}
}

in this example, the code would keep asking for treats/exercises till the user gets an amount of calories equals to 1500.

Steven204322 (2)

@RamonNeres Sorry I still don't get it, could I get you to implement it into the lines of code sorry I am so confused and I have no idea

RamonNeres (0)

Here, I forked your project and added the loop. Check if this is what you're looking for: https://repl.it/@RamonNeres/Mini-Project-1

Steven204322 (2)

@RamonNeres Thanks, that is exactly what I wanted, but the only thing now is that it does nothing when it equals to 1500 calories and it doesn't print the end statement, which is "Take it easy! Have a treat!" How do I do that?

Steven204322 (2)

@RamonNeres Wait no I got the statement to print just now but I don't want to to keep going I want it to stop after the first time she burns below 1500 calories like for example: she starts with 1400 and then eats a treat of 300 and then burns 500 then since it is below 1500 or equal to it, it will now end.

Steven204322 (2)

@RamonNeres Thank you so much by the way! You are great help! I think my last comment is the only thing left if you can please help with that.

RamonNeres (0)

@Steven204322 Ok. I changed the condition in the loop to (calories > 1500) instead of (calories != 1500), so now it will keep looping until the user get below or equals 1500 calories.
But it will create another problem. If the user inputs an amount of calories below 1500 at the start of the program, it will not enter the loop and the program will just finish.
To solve it, I changed the 'while' loop to a 'do while'. This makes the program enter the loop for the first time even if the condition is not satisfied.
I added an else before the if that checks if the calories consumed > 1500, this will prevent the program to enter that block if it passed throug the first if that checks if calories consumed < 1500.

You can check the changes here:https://repl.it/@RamonNeres/Mini-Project-1

If you can't understand the changes I made, you can keep asking me here :)