Advertisement
desislava_topuzakova

04. Walking начин 2

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