java如何重复userinput,如何重复某些操作,直到在Java中给出正确的输入?

I am trying to make a application and for one part of the application I need to get a input from the user stating how many times there click their mouse in 1 second. I want the input their give to be between 1-10 and any other number given e.g. 0, -1, 11 to provide them with a error and ask them to input a valid number of 1-10. Also if the user types in any character e.g. name, A, Jo or hello, to also provide them with a error and ask them to provide the correct input. Below is what I have but it does not work.

int OrginalMouseClick;

String Mouseclick = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");

int Mouseclick2 = Integer.parseInt(Mouseclick);

while (Mouseclick2 < 1 || Mouseclick2 > 10) {

String Mouseclick = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");

if (Mouseclick2 >= 1 || Mouseclick2 <10) {

OrginalMouseClick = Mouseclick2;

}

}

I haven't yet implemented not to accept any characters like name, j, A, hello because I am not sure how I can do this, can someone show me please.

edit:

int mouseClick;

do {

while (!str.hasNextInt()) {

String str = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");

str.next(); // this is important!

}

String str = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");

mouseclick = Integer.parseInt(str);

}

while (mouseclick < 1 || mouseclick > 10);

解决方案

An example as requested:

int mouseClick;

do {

String str = JOptionPane.showInputDialog("Write down how many times you can click your mouse button in 1 second");

mouseclick = Integer.parseInt(str);

}

while (mouseclick < 1 || mouseclick > 10);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值