desislava_topuzakova

3. Стъпки

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