Advertisement
veronikaaa86

04. Walking

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