这是我的程序的一部分,我想要求用户插入“true”或false“
但我继续java unexpected return value
,我只是找不到解决这个问题的方法,我想要return
main
方法的价值
private String Reply() {
Scanner input_reader = new Scanner(System.in);
System.out.print("true or false");
String keyboard = null;
keyboard = input_reader.nextLine();
while (keyboard != "True" | keyboard != "False") { //if the user dosent type true or false it will continue to ask for it
System.out.print("Please answer with T or F ");
Scanner newInput = new Scanner(System.in);
String a = newInput.nextLine();
keyboard = a;
return keyboard;
}
}
感谢所有的帮助!谢谢!
答案 0 :(得分:1)
您还希望将return语句移出while循环,因为在完成循环之前不应返回该值:
while (!(keyboard.trim().equalsIgnoreCase("True") || keyboard.trim().equalsIgnoreCase("False"))) { //if the user dosent type true or false it will continue to ask for it
System.out.print("Please answer with True or False ");
Scanner newInput = new Scanner(System.in);
String a = newInput.nextLine();
keyboard = a;
}
return keyboard;