Advertisement
GabrielHr00

04. Walking

Jun 11th, 2023
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. package S5_WhileLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Walking {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int goal = 10000;
  9.  
  10.         String command = scanner.nextLine();
  11.         while(!command.equals("Going home")) {
  12.             int steps = Integer.parseInt(command);
  13.             //goal = goal - steps;
  14.             goal -= steps;
  15.  
  16.             if(goal <= 0) {
  17.                 break;
  18.             }
  19.  
  20.             command = scanner.nextLine();
  21.         }
  22.  
  23.         if(command.equals("Going home")) {
  24.             int stepsToHome = Integer.parseInt(scanner.nextLine());
  25.             goal -= stepsToHome;
  26.         }
  27.  
  28.         if(goal > 0) {
  29.             System.out.printf("%d more steps to reach goal.", goal);
  30.         } else {
  31.             System.out.printf("Goal reached! Good job!%n%d steps over the goal!", Math.abs(goal));
  32.         }
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement