java怎么定义接收用户输入4个整数,不是4个整数就显示报错 刚入门,有大神能回答吗 百度没找到。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Scanner sc = new Scanner;sc.nextInt()接收呀 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 + "'有效"); } } }} public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); String str = input.nextLine(); String [] contents = str.split(input.delimiter().toString()); System.out.println("输入的内容'" + str + "'!"); System.out.println("输入的数量:'" +contents.length + "'!"); if(contents.length != 4){ System.out.println("请输入4个数字!"); } else{ for(int i = 0; i < contents.length; i++){ if(!contents[i].matches("^\\s*\\d+\\s*$")){ System.out.println("请输入4个数字!"); System.exit(1); }else{ System.out.println("输入第"+ (i + 1)+"的数字:" + contents[i] + "!"); } } } } 定义一个int类型的数组,长度为4,再用for循环里面写个接受语句,然后用try catch当长度不为4时给出提示语句 直接用nextInt()逐个读取然后保存起来,这些读取的语句用try catch语句包起来,如果出现异常就返回并提示报错,停止数据输入并停止程序。 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"); } }} 怎么使用Map接口获得查询结果? ant中如何使用jaxb? try 块中可以有方法吗? java 窗体交互问题谢谢 华为招聘 请各位高手帮忙,只是举手之劳,谢谢!!! 我是菜鸟,请问print和println有什么区别? java中有没有操作符重载? 我想买优惠的JAVA认证券,今年什么时候可以买到? Help! Java连接Access数据库 这个java程序编译时会显示i没有初始化呢? 关于volatile关键字与javabean的线程安全
sc.nextInt()接收呀
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 + "'有效");
}
}
}
}
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String str = input.nextLine();
String [] contents = str.split(input.delimiter().toString());
System.out.println("输入的内容'" + str + "'!");
System.out.println("输入的数量:'" +contents.length + "'!");
if(contents.length != 4){
System.out.println("请输入4个数字!");
}
else{
for(int i = 0; i < contents.length; i++){
if(!contents[i].matches("^\\s*\\d+\\s*$")){
System.out.println("请输入4个数字!");
System.exit(1);
}else{
System.out.println("输入第"+ (i + 1)+"的数字:" + contents[i] + "!");
}
}
}
}
// 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");
}
}
}