0%

超出范围重新录入

规定范围内录入数据

1.多位时(for循环和i++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) {
//输入规定范围的整数:0-100之间
int[] scores = new int[6];
Scanner sc =new Scanner(System.in);
for (int i = 0; i < scores.length;) {
System.out.println("请输入评分:");
int score=sc.nextInt();
if (score>=0&&score<=100){
scores[i]=score;
i++;
}else{
System.out.println("超出范围,继续录入,当前的i为:"+i);
}
}
}

2.一位时(while和break)

1
2
3
4
5
6
7
8
9
while (true) {
System.out.println("输入蓝色球号:");
int number = number = sc.nextInt();
if (number >= 1 && number <= 16) {
newArr[newArr.length-1] = number;
break;
}else{
System.out.println("蓝球码超出范围,重新输入:");
}