Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package WhileLoop;
- import java.util.Scanner;
- public class Steps_03 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int targetSteps = 10000; //целеви стъпки за ден
- int totalSteps = 0; //извървяни стъпки
- //1. ако постигне целта си: totalSteps >= targetSteps
- //2. ако се въведе Going home
- //повтарям: 1. прочитам команда -> Going home или стъпки
- //спирам: totalSteps >= targetSteps
- //продължавам: totalSteps < targetSteps
- while (totalSteps < targetSteps) {
- String command = scanner.nextLine();
- if (command.equals("Going home")) {
- int stepsToHome = Integer.parseInt(scanner.nextLine());
- totalSteps += stepsToHome;
- break;
- } else { //стъпки под формата на текст ("1000")
- int steps = Integer.parseInt(command);
- totalSteps += steps;
- }
- }
- //спирам да вървя
- if (totalSteps >= targetSteps) { //постигнала целта си
- System.out.println("Goal reached! Good job!");
- System.out.printf("%d steps over the goal!", totalSteps - targetSteps);
- } else { //не е постигнала целта си -> totalSteps < targetSteps
- System.out.printf("%d more steps to reach goal.", targetSteps - totalSteps);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment