Scanner输入
import java.util.Scanner;
public class ScannerTest {
public static void main(String[] args) {
/*
* 1.从键盘中接受一个字符,从键盘中键入的任何两个输入值之间都必须用空白字符分隔开,
* 2.输入行的结尾由转义字符'\n'表示,当我们在键盘中按下Enter键时,就输入了'\n'字符
* 3.next和nextLine方法的区别:next()方法是不接收空格的,在接收到有效数据前,所有的空格或者tab键等输入被忽略,若有有效数据,则遇到这些键退出。
* nextLine()可以接收空格或者tab键,其输入应该以enter键结束。
* 4.语法:方法nextInt、nextDouble、next分别用来读取并返回int类型的数据,double类型的数据以及作为String对象的词
* 5.需要导入头文件:java.util.Scanner
*/
Scanner s=new Scanner(System.in);//输入
sc.nextLine;
System.out.println();
}
}
BufferedReader输入
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class BufferedReaderTest {
public static void main(String[] args) throws IOException {
//缓冲输入
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//while((line = br.readLine)!=-1)
String[] line = br.readLine().split(" ");
int n = Integer.parseInt(line[0]), m = Integer.parseInt(line[1]);
int[][] nums = new int[n][m];
for ( int i = 0; i < n; i++ ) {
line = br.readLine().split(" ");
for ( int j = 0; j < m; j++ ) {
nums[i][j] = Integer.parseInt(line[j]);
}
}
}
}
StreamTokenizer输入
nextToken() - 分析下一个。
String toString() 返回当前流标记及其发生的行号的字符串表示形式。
//普通用法
StreamTokenizer st =new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
st.nextToken(); // 获取下一组标记 默认是按照空格分割的 回车,tab是结束符
int i=(int) st.nval; //st.navl默认解析出的格式是double
st.nextToken();
double j=st.nval;
st.nextToken();
String s=st.sval;
//多组输入
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int a, b;
while(in.nextToken() != StreamTokenizer.TT_EOF) // 表示读到了文件末尾
{
a = (int)in.nval;
in.nextToken();
b = (int)in.nval;
//out.println(a + b);
System.out.println("a + b = "+(a+b));
}
//out.flush();
}
}
直接输出
System.out.println() //会换行
System.out.print() //不会换行
StringBuilder输出
//StringBuilder输出
StringBuilder sb = new StringBuilder();
for ( int i = 0; i<n; i++) {
for ( int j = 0; j < m; j++ ) {
sb.append(nums[i][j]+" ");
}
sb.append("\n");
}
System.out.println(sb.toString());
}
PrintWriter输出
// PrintWriter输出
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
for ( int i = 0; i<n;i++) {
for ( int j = 0; j < m; j++ ) {
out.print(nums[i][j] + " ");
}
out.println();
}
out.flush();
}