galinyotsev123

ProgBasics05while-Loop-P05walking

Jan 7th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class P05walking {
  4. public static void main(String[] args) {
  5.  
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. int goal = 10000;
  9. int steps = 0;
  10.  
  11. while (true) {
  12. String command = scanner.nextLine();
  13. if (command.equals("Going home")) {
  14. int num = Integer.parseInt(scanner.nextLine());
  15. steps += num;
  16. break;
  17. }
  18. int num = Integer.parseInt(command);
  19. steps += num;
  20. if (steps >= goal) {
  21. break;
  22. }
  23. }
  24. if (steps >= goal) {
  25. System.out.println("Goal reached! Good job!");
  26. } else {
  27. System.out.printf("%d more steps to reach goal.", (goal - steps));
  28. }
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment