它编译,只是它不会初始化while (choice = false)
因此无论输入什么ans
,它都不会显示“Invalid input,enter a,b,c:”并重申。布尔方法不返回false
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String ans;
boolean choice;
System.out.print("Enter a, b, c: ");
ans = kb.nextLine();
choice = isValidChoice(ans);
while (choice = false)
{
System.out.print("Invalid input, enter a, b, c: ");
ans = kb.nextLine();
choice = isValidChoice(ans);
}
if (choice = true)
{
System.out.println("Your input was " + ans);
}
}
public static boolean isValidChoice(String choice)
{
if (choice.equalsIgnoreCase("a") || choice.equalsIgnoreCase("a")
|| choice.equalsIgnoreCase("a"))
{
return true;
}
else
{
return false;
}
}
}
我想你可能想'=='而不是'='。比较与分配。更好的是,你可以使用'while(choice)'这个检查是否为true或'while(!choice)'这个检查是否为false。 – csmckelvey 2014-11-25 03:27:47
我还注意到了一件事,但它不是问题的原因。你有三个choice.equalsIgnoreCase(“a”)..只是为了引起你的注意。 – 2014-11-25 03:48:49