myrdok123

P04Walking

Feb 5th, 2023
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. package L05WhileLoop.Exercises;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04Walking {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         //Прочитаме входните данни -> стринг
  11.         //while -> !command.equals("Going home")
  12.             // прочитаме колко стъпки е извървяла за 1 излизане
  13.  
  14.         String command = scanner.nextLine();
  15.         int countSteps = 0;
  16.  
  17.         while (!command.equals("Going home")){
  18.             int currentSteps = Integer.parseInt(command);
  19.  
  20.             countSteps += currentSteps;
  21.  
  22.             if (countSteps >= 10000){
  23.                 break;
  24.             }
  25.             command = scanner.nextLine();
  26.         }
  27.  
  28.         if(command.equals("Going home")){
  29.             int stepsToHome = Integer.parseInt(scanner.nextLine());
  30.             countSteps += stepsToHome;
  31.         }
  32.  
  33.         if (countSteps < 10000){
  34.             System.out.printf("%d more steps to reach goal.", 10000 - countSteps);
  35.         }else {
  36.             System.out.println("Goal reached! Good job!");
  37.             System.out.printf("%d steps over the goal!", countSteps - 10000);
  38.         }
  39.     }
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment