如何停止无限重复的 else?在 while (true) 循环中,else 函数会无限重复。Java

huangapple go评论78阅读模式
英文:

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

问题

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!");
                break; // Exit the loop after successful login
            } else {
                System.out.println("Please write a word.");
                addauthentication = obj.next(); // Read new input within the loop
            }
        }
    }
}
英文:

I want to make a test code - Confirmation that you are not a robot.
I declare a String variable named 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 addauthentication.equals(// that shortest word) - print (you are logged into your account!).

Else
("Please write the word").

The if else function using a while loop will repeat as long as the condition is true.

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.

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.


</details>


# 答案1
**得分**: 2

好的,以下是翻译好的部分:

好的,如果我理解正确,您希望在用户给出正确答案之前保持循环。并且您不希望“请写一个词”一直显示下去。
这是因为您只从控制台读取一次(在循环外部),所以完全相同的字符串会一遍又一遍地进行评估,如果第一次就错了,它将始终错误。
因此,我的建议是在循环内部进行读取,这样您将在每次迭代中检查答案。

```java
while (true) {
    addauthentication = obj.next();
    if (addauthentication.equals("և")){
        System.out.println("您已成功登录您的账户!");
        break;
    }
    else{
        System.out.println("请写一个词。");
    }
}

正如您所看到的,我已经添加了break语句,以便在用户输入正确答案时退出循环。

英文:

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(&quot;և&quot;)){
        System.out.println(&quot;You are logged into your account!&quot;);
        break;
    }
    else{
        System.out.println(&quot;Please, write a word.&quot;);
    }
}

As you can see, I have added a break in order to exit the loop whenever the user hits the right answer.

huangapple
  • 本文由 发表于 2020年7月28日 21:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63135282.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定