Advertisement
myrdok123

04. Walking

Feb 4th, 2024
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1. package W05WhileLoop.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.         String command = scanner.nextLine();
  11.  
  12.         int sumSteps = 0;
  13.  
  14.         //Правим While цикъл, докато не получим команда "Going Home"
  15.         while (!command.equals("Going home")){
  16.  
  17.             //Превръщаме прочетените стъпки от стринг в инт
  18.             int currentSteps = Integer.parseInt(command);
  19.             sumSteps += currentSteps;
  20.  
  21.             //Проверяваме дали сме постигнали целта от 10000 крачки
  22.             if(sumSteps >= 10000){
  23.                 break;
  24.             }
  25.  
  26.             command = scanner.nextLine();
  27.  
  28.         }
  29.  
  30.         //Проверяваме дали имаме команда Going Home
  31.         if(command.equals("Going home")){
  32.             int stepsToHome = Integer.parseInt(scanner.nextLine());
  33.             sumSteps += stepsToHome;
  34.         }
  35.  
  36.         if (sumSteps >= 10000){
  37.             System.out.println("Goal reached! Good job!");
  38.             System.out.printf("%d steps over the goal!", sumSteps - 10000);
  39.         }else {
  40.             System.out.printf("%d more steps to reach goal.", 10000 - sumSteps);
  41.         }
  42.  
  43.     }
  44. }
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement