Advertisement
myrdok123

04. Walking

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