Java输入输出示例
//输入一组数据并输出
Scanner sc = new Scanner(System.in);
String str=sc.nextLine();
System.out.printf("%s
",str);
//输入多组数据并输出
Scanner sc = new Scanner(System.in);
String str;
while(sc.hasNextLine()){
str = sc.nextLine();
System.out.printf("%s
",str);
}
样例
import java.io.*;
import java.util.*;
class Test {
}
public class Main
{
public static void main(String args[])
{
Scanner cin = new Scanner(System.in);
int a, b;
while(cin.hasNextInt())
{
a = cin.nextInt();
b = cin.nextInt();
System.out.println(a + b);
}
}
}
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
}
}
多行数据示例:
三行:第一行为集合A和B的长度,第二行为集合A,第三行为集合B;示例:
1 2
1
2 3
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int m = sc.nextInt();
int[] arrayN = new int[n];
int[] arrayM = new int[m];
for (int i = 0; i < n; i++) {
arrayN[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
arrayM[i] = sc.nextInt();
}
}
}
}