如果我错了,很抱歉,我已经环顾四周,并没有找到任何直接回答我问题的问题。我正在写一个应该记录你的出生信息的程序,年,月,日,时,分,然后问他们是否愿意再做一次,但是使用方法和循环直到Y / N为止输入
我的问题是我无法让方法接受Y / N并结束程序,以及在输入N时结束。
我认为有一段时间或某种循环需要在方法中,但我很难搞清楚。
我的所有其他方法除了这个之外都有效,感谢任何帮助,谢谢。
这是我现在的方法代码:
public static boolean getYNConfirm(Scanner pipe, String prompt)
{
String choice="";
System.out.println(prompt);
choice = pipe.nextLine();
if(choice.equalsIgnoreCase("Y"))
{
return true;
}
else
{
return false;
}
}
这是我主程序的代码:
public static void main(String[] args)
{
int year, month, day, hour, minutes;
String msg="";
boolean done = false;
Scanner in = new Scanner(System.in);
while(!done)
{
year = SafeInput.getIntInRange(in, "Enter the year you were born: ", 1965, 2000);
month = SafeInput.getIntInRange(in, "Enter your month of birth: ", 1, 12);
switch (month)
{
case 1:
msg = "January";
break;
case 2:
msg = "February";
break;
case 3:
msg = "March";
break;
case 4:
msg = "April";
break;
case 5:
msg = "May";
break;
case 6:
msg = "June";
break;
case 7:
msg = "July";
break;
case 8:
msg = "August";
break;
case 9:
msg = "Septemeber";
break;
case 10:
msg = "October";
break;
case 11:
msg = "November";
break;
case 12:
msg = "December";
break;
}
hour = SafeInput.getIntInRange(in, "Enter the hour you were born in: ", 1, 24);
minutes = SafeInput.getIntInRange(in, "Enter the minutes you were born: ", 1, 59);
System.out.println("You were born: " + year + " , " + msg + " , " + hour + " hr. " + minutes + " mins. ");
SafeInput.getYNConfirm(in, "Would you like to play again?");
}
}
}
感谢您的帮助。
答案 0 :(得分:3)
done
的值始终为false
。最后一行改变:
done = SafeInput.getYNConfirm(in, "Would you like to play again?");
编辑:
如果您返回true
,您的逻辑也不合适:
if(choice.equalsIgnoreCase("Y"))
{
return false;
}
else
{
return true;
}
答案 1 :(得分:0)
你需要在getYNConfirm方法的返回时更改done base的值。所以最后一行应该是
done = SafeInput.getYNConfirm(in, "Would you like to play again?");