【问题标题】:How to stop the eternal repetition of else? The else function repeats forever in a while (true) loop. Java如何阻止 else 的永恒重复? else 函数在 while (true) 循环中永远重复。爪哇
【发布时间】:2020-07-28 13:28:57
【问题描述】:

我想做一个测试代码 - 确认你不是机器人。 我声明了一个名为 addauthentication 的字符串变量。 接下来,我添加 if else 函数。系统会要求你用你的语言写最短的单词(在我的例子中是亚美尼亚语)。 if addauthentication.equals(// 最短的单词) - 打印(您已登录到您的帐户!)。

其他 (“请写下单词”)。

只要条件为真,使用 while 循环的 if else 函数就会重复。

假设我的答案不正确,那么程序应该向我显示:(请写一个单词)。但同样的短语永远存在。但是,如果我的第一个答案是正确的,系统会显示这个(您已登录到您的帐户!) - 是的,这个想法没有问题。如何更正我的代码,以便在回答错误后,我可以输入正确的代码,并且短语(请写一个单词)不会重复?

我的代码很轻,似乎不应该有任何错误。特别是,我在 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.

【问题讨论】:

  • if 条件为真时,您忘记了循环中的break
  • 顺便说一句:“我的代码很轻,看起来应该没有任何错误。”简短并不意味着正确。
  • 另外,请参阅this question,了解如何询问输入直到正确为止。
  • 只在 while 内执行 addauthentication = obj.next();,在 if-then 部分执行 break。太轻的代码也是……

标签: java loops if-statement


【解决方案1】:

好的,所以如果我理解得很好,您希望留在循环中,直到用户给出正确答案。而且您不希望永远显示“请写一个词”。 发生这种情况是因为您从控制台(循环外)只读取一次,所以完全相同的字符串会一遍又一遍地评估,如果第一次出错,它会一直出错。 所以我的建议是在循环内部阅读,这样你就可以在每次迭代中检查答案。

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.");
    }
}

如您所见,我添加了一个break,以便在用户点击正确答案时退出循环。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 2015-08-08
    • 1970-01-01
    • 2022-12-07
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    • 2015-12-28
    • 1970-01-01
    相关资源
    最近更新 更多