简体   繁体   English

如何停止 else 的永恒重复? else function 在 while (true) 循环中永远重复。 Java

[英]How to stop the eternal repetition of else? The else function repeats forever in a while (true) loop. Java

I want to make a test code - Confirmation that you are not a robot.我想做一个测试代码——确认你不是机器人。 I declare a String variable named addauthentication.我声明了一个名为 addauthentication 的字符串变量。 Next, I add the if else function. The system will ask you to write the shortest word in your language (in my case, in Armenian).接下来,我添加 if else function。系统会要求您用您的语言(在我的例子中,用亚美尼亚语)写出最短的单词。 If addauthentication.equals(// that shortest word) - print (you are logged into your account.). If addauthentication.equals(// that shortest word) - 打印(您已登录到您的帐户。)。

Else ("Please write the word").否则(“请写下这个词”)。

The if else function using a while loop will repeat as long as the condition is true.只要条件为真,使用 while 循环的 if else function 就会重复。

Suppose my answer was not correct, then the program should display this to me: (Please write a word).假设我的答案不正确,那么程序应该向我显示:(请写一个词)。 But the same phrase runs forever.但同样的短语永远流传。 But, if my first answer were correct, the system would show me this (you are logged into your account,) - and yes.但是,如果我的第一个答案是正确的,系统会向我显示这个(您已登录到您的帐户)- 是的。 there were no problems with this thought, How can I correct my code so that after an incorrect answer, I could enter the correct one?这个想法没有问题,我怎样才能更正我的代码,以便在错误答案之后,我可以输入正确的答案? and the phrase (Please write a word) is not repeated?并且短语(请写一个词)不重复?

My code is very light, it seemed there shouldn't have been any errors.我的代码很轻,看起来不应该有任何错误。 And in particular, I cannot find the answers to my question in StackOverFlow, so I was forced to ask you my question.特别是,我无法在 StackOverFlow 中找到我的问题的答案,所以我不得不问你我的问题。

import java.util.Scanner;
    
public class RobotTest2 {
    public static void main(String []args) {
        String addauthentication;
        Scanner obj = new Scanner(System.in);
           
        System.out.println("Confirm with action, that you are not a robot. Write the shortest word in your language.");
        addauthentication = obj.next();
    
        while (true) {
            if (addauthentication.equals("և")) {
                System.out.println("You are logged into your account!");
            } else  
                System.out.println("Please, write a word.");
            }
        }   
    }



My expected result:

    > Confirm with action, that you are not a robot. Write the shortest word in your language.
    > 
    > // user input "և"
    > 
    > You are logged into your account!






//other way

    

> > Confirm with action, that you are not a robot. Write the shortest word in your language.
>     > 
>     > // user input //wrong answer
>     > 
>     > Please write a word. 
>     //user input ("right answer")
>     "You are logged into you account!"



The real result:

   

>  > Confirm with action, that you are not a robot. Write the shortest word in your language.
>     > 
>     > // user input "և"
>     > 
>     > You are logged into your account!




//Other way

    

> > >  Confirm with action, that you are not a robot. Write the shortest word in your language.
> >     
> >              //user input 
> >             //wrong answer 
> >             
>              "Please write a word." 
>          "Please write a word." 
>          "Please write a word." 
>          "Please write a word." 
>          "Please write a word." 
>          "Please write a word." 
>         ......

//And so, the same phrase repeats forever.

Ok, so if I understood well, you want to stay in the loop until the user gives the correct answer.好的,所以如果我理解得很好,你想留在循环中直到用户给出正确答案。 And you don't want "Please write a word" to be shown forever.而且您不希望“请写一个字”永远显示。 This is happening because you're reading only once from the console (outside the loop) so the exact same string is evaluating over and over again, if it was wrong the first time, it'll keep wrong.发生这种情况是因为您只从控制台(循环外)读取一次,所以完全相同的字符串会一遍又一遍地评估,如果第一次出错,它会一直出错。 So my suggestion is to read inside the loop, so you'll check the answer in every iteration.所以我的建议是在循环内部阅读,这样你就会在每次迭代中检查答案。

while (true) {
    addauthentication = obj.next();
    if (addauthentication.equals("և")){
        System.out.println("You are logged into your account!");
        break;
    }
    else{
        System.out.println("Please, write a word.");
    }
}

As you can see, I have added a break in order to exit the loop whenever the user hits the right answer.如您所见,我添加了一个break ,以便在用户点击正确答案时退出循环。

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

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