Advertisement
GabrielHr00

04. Walking

Apr 7th, 2024
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.85 KB | None | 0 0
  1. package _05_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.  
  11.         while (goal > 0) {
  12.             String input = scanner.nextLine();
  13.             if (input.equals("Going home")) {
  14.                 int tiredSteps = Integer.parseInt(scanner.nextLine());
  15.                 goal -= tiredSteps;
  16.                 break;
  17.             }
  18.  
  19.             int steps = Integer.parseInt(input);
  20.  
  21.             goal = goal - steps;
  22.             // goal -= steps;
  23.         }
  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.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement