Y
Y
Yulik2017-08-05 23:47:29
Java
Yulik, 2017-08-05 23:47:29

How to finish the code so that everything works and when you enter "n" the program ends?

Does not work, when you enter "n" nothing happens.
It should end the program when you enter "n" (there is no code after the cycle), and when you press "y" return to the beginning of the cycle.
Please help
while(true){
//HERE
//CODE
String input = scanner.next();
if (input == "n") {break;}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vante_scribaxxi, 2017-08-06
@vante_scribaxxi

I haven't written in java for a long time. I think something like this:

String str=""
while(!str.equals("n")){
    //КОД
    str = scanner.next();
}

Although you can try this:
while(!scanner.next().equals("n")){
  //код
}

D
Dmitry Podbolotov, 2017-08-07
@Cynep_3APA3A

Strings are objects, which means you can only compare using equal()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question