gabri2503
gabri2503

Reputation: 36

How to not print everything inside a loop but make it based on decisions

so I have a question to Java. I want to print everything from the method once and not every time. The method is called refrigeratorInformation. So my question is, how do I just run the methode once and then get asked again, what I want to do next. Here is the code:

System.out.println("State what you would like to do with the refrigerator:");
USER_INPUT = input.nextLine();
while(true){
     if (USER_INPUT.equalsIgnoreCase("I want to close the refrigerator")){
         TimeUnit.SECONDS.sleep(1);
         System.out.println("Shutting down!");
         TimeUnit.SECONDS.sleep(3);
         System.exit(0);
     } else if (USER_INPUT.equalsIgnoreCase("What is the current temperature inside the refrigerator")){
         refrigeratorTemperature();
     } else if (USER_INPUT.equalsIgnoreCase("Show me some info about the refrigerator")){
         refrigeratorInformation();
     }            
}

And here is the methods code:

public void refrigeratorInformation(){
    dimension= "Width is 178cm, Height 66,8cm & length is 59,5cm";
    usage = 157;
    volume = 707.5; // in liter
    name = "Build Your Body Fat";
    weight = 63;
    try{
        System.out.println(name);
        System.out.println(weight);
        System.out.println(volume +" Liter");
        System.out.println("The refrigerator has a usage of " + usage + "kWh");
        System.out.println(dimension);
        TimeUnit.SECONDS.sleep(5);
    } 
...

I would be pretty thankful, if you could help me out

Upvotes: 0

Views: 418

Answers (1)

Rainmaker
Rainmaker

Reputation: 11090

If you want to stop executing the method you put the return statement in the end of this method, if you want to skip an iteration you put continue in your loop skipping one iteration. And if you put break your loop stops and not the method. I believe you're looking for break

Read the docs while loop

Upvotes: 2

Related Questions