mardi 28 juillet 2020

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. 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.

Aucun commentaire:

Enregistrer un commentaire