简体   繁体   English

如何停止while循环而不退出它(Java)

[英]How do I stop a while loop without exiting it (Java)

Hello so I have a question to Java (I'm new so dont excpext too much). 您好,所以我对Java有一个疑问(我是新手,所以请不要过多地尝试)。 I want to just print everything from the method once and not the whole time. 我想只打印一次方法中的所有内容,而不是整个时间。 The method is called 'kuehlschrankInformationen'. 该方法称为“ kuehlschrankInformationen”。 So my question is, how do I just run the methode once and then he starts me asking again, what I want to do. 所以我的问题是,我该如何运行方法一次,然后他又让我再次询问我想做什么。 Here is the code(the text is german but I guess it wont make any difference): 这是代码(文本为德语,但我想不会有任何区别):

        System.out.println("Geben Sie ein, was Sie mit dem Kühlschrank machen wollen:");
        USER_INPUT = input.nextLine();
        while(true){
            if (USER_INPUT.equalsIgnoreCase("Ich möchte meinen Kühlschrank schließen")){
                TimeUnit.SECONDS.sleep(1);
                System.out.println("Das System wird nun herunter gefahren, bis bald");
                TimeUnit.SECONDS.sleep(3);
                System.exit(0);
            }
            else if (USER_INPUT.equalsIgnoreCase("Was ist die derzeitige Temperatur im Kühlschrank")){
                kuehlschrankTemperatur();
            }
            else if (USER_INPUT.equalsIgnoreCase("Zeigen Sie mir Informationen über den Kühlschrank an")){
                kuehlschrankInformationen();
            }


        }

And here is the methods code: 这是方法代码:

public void kuehlschrankInformationen(){
    dimensionen = "Die Breite beträgt 178cm, die Höhe 66,8cm & die Länge 59,5cm";
    verbrauch = 157;
    volumen = 707.5; // in liter
    name = "Build Your Body Fat";
    gewicht = 63;
    try{
        System.out.println(name);
        System.out.println(gewicht);
        System.out.println(volumen +" Liter");
        System.out.println("Der Kühlschrank verbraucht " + verbrauch + "kWh");
        System.out.println(dimensionen);
        TimeUnit.SECONDS.sleep(5);

I would be pretty thankful, if you could help me out 如果您能帮助我,我将非常感激

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. 如果要停止执行该方法,则将return语句放在该方法的末尾,如果要跳过迭代,则将continue循环跳过一个迭代。 And if you put break your loop stops and not the method. 而且,如果您break了,则循环停止,而不是方法。 I believe you're looking for break 我相信你正在寻找break

Read the docs while loop 阅读文档while循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM