Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package S5_WhileLoops;
- import java.util.Scanner;
- public class Walking {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // save all steps here
- int stepsSum = 0;
- String command = scanner.nextLine();
- while(!command.equals("Going home")) {
- // steps count when going out
- int steps = Integer.parseInt(command);
- stepsSum = stepsSum + steps;
- // check if goal is reached
- if(stepsSum >= 10000) {
- break;
- }
- // read command or steps once again
- command = scanner.nextLine();
- }
- // check if we should walk home
- if(command.equals("Going home")) {
- int stepsToHome = Integer.parseInt(scanner.nextLine());
- stepsSum = stepsSum + stepsToHome;
- }
- // check if we reached our walking goal
- int diff = Math.abs(10000 - stepsSum);
- if(stepsSum >= 10000) {
- System.out.printf("Goal reached! Good job!%n%d steps over the goal!", diff);
- } else {
- System.out.printf("%d more steps to reach goal.", diff);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement