Advertisement
veronikaaa86

04. Walking

Feb 6th, 2022
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. package whileLoop;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class P04Walking {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. int targetSteps = 10000;
  10.  
  11. int reachedSteps = 0;
  12. String command = scanner.nextLine();
  13. while (!command.equals("Going home")) {
  14. int currentSteps = Integer.parseInt(command);
  15.  
  16. reachedSteps = reachedSteps + currentSteps;
  17.  
  18. if (reachedSteps >= targetSteps) {
  19. break;
  20. }
  21.  
  22. command = scanner.nextLine();
  23. }
  24.  
  25. if (command.equals("Going home")) {
  26. int homeSteps = Integer.parseInt(scanner.nextLine());
  27. reachedSteps = reachedSteps + homeSteps;
  28. }
  29.  
  30. int diff = Math.abs(reachedSteps - targetSteps);
  31. if (reachedSteps >= targetSteps) {
  32. System.out.println("Goal reached! Good job!");
  33. System.out.printf("%d steps over the goal!%n", diff);
  34. } else {
  35. System.out.printf("%d more steps to reach goal.", diff);
  36. }
  37. }
  38. }
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement