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