我无法让 do-while 循环成功地将整数更改为 if 语句中的值。如果我输入' y '或' n ',它将正确退出循环,但整数的值将保持为0。
我使用子字符串来允许用户输入诸如“ yes ”或“ YEs ”之类的内容,甚至“ Y3$ir ”,并且仍然等同于java的“ y ”。
代码:
import java.util.Scanner;
public class aTaskforAll
{
public static void main (String [] args)
{
Scanner scan = new Scanner(System.in);
String readAll;
int readAllOption = 0;
do {
System.out.print("Do you want the words printed? (y/n) ");
readAll = scan.nextLine();
System.out.println(readAll.substring(0));
if ((readAll.substring(0) == "y") || (readAll.substring(0) == "Y"))
readAllOption = 1;
else if ((readAll.substring(0) == "n") || (readAll.substring(0) == "N"))
readAllOption = 2;
}
while (readAllOption != 0);
System.out.println(readAllOption); //Tester
//Go on to do task in response to readAllOption = 1 or 2
}
}