silviasj

walking

Apr 4th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.33 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.         //СТОП: стъпки >= 10000 -> продължава:стъпки < 10000
  8.         //СТОП: команда = "Going home"
  9.  
  10.         int steps = 0;
  11.         boolean isGoingHome = false;
  12.         //false -> не отива към вкъщи, true -> отива към вкъщи
  13.         while(steps < 10000){
  14.  
  15.             String command = scanner.nextLine(); // Going home или число, което са стъпките
  16.             if(command.equals("Going home")){
  17.                 int stepsToHome = Integer.parseInt(scanner.nextLine());
  18.                 steps += stepsToHome;
  19.                 isGoingHome = true;
  20.                 break;
  21.             } else {
  22.                 //"1500" -> стринг към инт -> парсване
  23.                 int walkedSteps = Integer.parseInt(command);
  24.                 steps += walkedSteps;
  25.  
  26.             }
  27.         }
  28.  
  29.         //ако постигне целта си
  30.         if(steps >= 10000){
  31.             System.out.println("Goal reached! Good job!");
  32.         } else if(isGoingHome){
  33.             int leftSteps = 10000 - steps;
  34.             System.out.printf("%d more steps to reach goal.", leftSteps);
  35.         }
  36.  
  37.  
  38.  
  39.     }
  40. }
Add Comment
Please, Sign In to add comment