1.给定要输入的数据的个数,这个好办,用for循环输入即可
2.没有给定要输入数据的个数,这种一般需要输入字符串,再将字符串转成我们想要的类型
利用lambda表达式来将字符串数组转为相应的类型:
Scanner sc=new Scanner(System.in);
String nextLine = sc.nextLine();
String[] split = nextLine.split(",");
int b[]=Arrays.asList(split).stream().mapToInt(Integer::parseInt).toArray();
3.更多的例子
例子1:
2
1 2 1
2 2 1
Scanner cin=new Scanner(System.in);
int T,c;
long n,m;
while (cin.hasNext()) {
T=cin.nextInt();
for (int i = 0; i < T; i++) {
n=cin.nextInt();
m=cin.nextInt();
c=cin.nextInt();
System.out.println((n/2+m/2)<=c?"Yes":"No");
}
}
例子2:
4 3
1 1 2 3
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(), m = scanner.nextInt();
int[] sds = new int[n];
for (int i = 0; i < sds.length; i++) {
sds[i] = scanner.nextInt();
}