Advertisement
GabrielHr00

Walking

Nov 27th, 2022
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. package S5_WhileLoops;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Walking {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8. // save all steps here
  9. int stepsSum = 0;
  10.  
  11. String command = scanner.nextLine();
  12. while(!command.equals("Going home")) {
  13. // steps count when going out
  14. int steps = Integer.parseInt(command);
  15. stepsSum = stepsSum + steps;
  16.  
  17. // check if goal is reached
  18. if(stepsSum >= 10000) {
  19. break;
  20. }
  21. // read command or steps once again
  22. command = scanner.nextLine();
  23. }
  24.  
  25. // check if we should walk home
  26. if(command.equals("Going home")) {
  27. int stepsToHome = Integer.parseInt(scanner.nextLine());
  28. stepsSum = stepsSum + stepsToHome;
  29. }
  30.  
  31. // check if we reached our walking goal
  32. int diff = Math.abs(10000 - stepsSum);
  33. if(stepsSum >= 10000) {
  34. System.out.printf("Goal reached! Good job!%n%d steps over the goal!", diff);
  35. } else {
  36. System.out.printf("%d more steps to reach goal.", diff);
  37. }
  38.  
  39.  
  40. }
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement