java中y n设置_Java - 循环直到输入Y或N的方法(Java - Method to loop until a Y or N is entered)...

I have looked around and haven't found any questions that has been directly answered for my problem, if i'm wrong, sorry. I'm writing a program that is supposed to take in your birth information, year, month, day, hour, minute and then ask if they would like to do it again but using a method and loop until a Y/N is entered.

My problem is im not able to get the method to take in the Y/N and end the program, as well as end when a N is entered.

I think there is supposed a while or some sort of loop that needs to be in the method but im having a hard time figuring that out.

All my other methods work except for this one, any help would be appreciated, thanks.

This is the code I have for my method now:

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;

}

}

And this is the code for my main program:

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?");

}

}

}

Thanks for any help.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值