Advertisement
desislava_topuzakova

04. Walking начин 1

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