Input 如何将输入整数限制在1到9之间
我有一个用于点球决战游戏的Java程序,我想限制b1、b2、b3和s1为1-9之间的任意数字。如果输入不等于1-9,请重试Input 如何将输入整数限制在1到9之间,input,int,limit,Input,Int,Limit,我有一个用于点球决战游戏的Java程序,我想限制b1、b2、b3和s1为1-9之间的任意数字。如果输入不等于1-9,请重试 for (int i=0;i<=5;i++) { int b1, b2, b3, s1; int j=i+1; System.out.println("Enter the Numbers which you will block.... Please remember tha
for (int i=0;i<=5;i++)
{
int b1, b2, b3, s1;
int j=i+1;
System.out.println("Enter the Numbers which you will block.... Please remember that it should be from 1-9 and nothingelse, 1 2 3 4 5 6 7 8 9");
b1=Integer.parseInt(br.readLine()); // how to restrict this to numbers between 1 to 9
b2=Integer.parseInt(br.readLine()); //this also
b3=Integer.parseInt(br.readLine()); //this also
System.out.flush();
System.out.println("Enter The Number where you will score");
s1=Integer.parseInt(br.readLine()); //this also
for(int i=0;i伪代码:
if ((input > 9) || (input < 1)) {
tryAgain()
}
if((输入>9)| |(输入<1)){
tryAgain()
}
我认为这就是您所需要的(不过您必须检查每个变量)。尝试这种动态方式
public class testJava {
public static void main(String[] args) {
testJava j = new testJava();
System.out.println(j.isBetween(1,1,9));
}
public boolean isNumeric(int value,int startValue,int endValue) {
if(value >= startValue || value <= endValue)
{
return true;
}else{
return false;
}
}
}
公共类testJava{
公共静态void main(字符串[]args){
testjavaj=newtestjava();
系统输出println(j.isBetween(1,1,9));
}
公共布尔值为数值(int值、int起始值、int结束值){
如果(值>=startValue | |值只需检查输入
If (number >= 1 && number <= 9)
{
progressFurther();
}
else
{
enterAgain();
checkAgain();
}
如果(number>=1&&number只需使用一个简单的条件,如:
if (x >= 1 && x <= 9) {
// handle the correct case
} else {
// handle the case where the input is not in the range 1-9
}
if(x>=1&&xwhile(b1>9 | | b1<1){
System.out.println(“错误的号码,再试一次。”);
b1=整数.parseInt(br.readLine());
}
while(b1 > 9 || b1 < 1) {
System.out.println("Wrong number. Try again.");
b1=Integer.parseInt(br.readLine());
}