Advertisement
GabrielHr00

04. Walking

Feb 2nd, 2025
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. package whileLoop_Exercise;
  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.  
  9.         int goal = 10000;
  10.  
  11.         while (goal > 0) {
  12.             String input = scanner.nextLine();
  13.  
  14.             if (input.equals("Going home")) {
  15.                 int stepsToHome = Integer.parseInt(scanner.nextLine());
  16.                 goal -= stepsToHome;
  17.                 break;
  18.             }
  19.  
  20.             int steps = Integer.parseInt(input);
  21.  
  22.             // goal = goal - steps;
  23.             goal -= steps;
  24.         }
  25.  
  26.         if (goal <= 0) {
  27.             System.out.printf("Goal reached! Good job!%n" +
  28.                     "%d steps over the goal!", Math.abs(goal));
  29.         } else {
  30.             System.out.printf("%d more steps to reach goal.", goal);
  31.         }
  32.  
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement