62,614
社区成员
发帖
与我相关
我的任务
分享
import java.util.Scanner;
public class test7 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
int count = 1;
int[] s = new int[4];
while (count < 5) {
System.out.println("请输入第" + count + "个整数");
try {
s[count - 1] = sc.nextInt();
count++;
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("输入不正确请重新输入");
sc.next();
}
}
for (int i = 0; i < 4; i++) {
System.out.print(s[i] + "\t");
}
}
}
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int time = 0;
String content = null;
while(time ++ < 4){
System.out.print("第" + time + "次输入一个整数:");
content = keyboard.nextLine();
if(!content.matches("^\\s*\\d+\\s*$")){
System.out.println("输入的内容'" + content + "'无效");
System.exit(1);
}else{
System.out.println("输入的内容'" + content + "'有效");
}
}
}
}