- 输入:
格式一 : Scanner sc = new Scanner (System.in);
读一个整数: int n = sc.nextInt();
读一个字符串:String s = sc.next();
读一个浮点数:double t = sc.nextDouble();
读一整行: String s = sc.nextLine();
判断是否有下一个输入可以用sc.hasNext()或sc.hasNextInt()或sc.hasNextDouble()或sc.hasNextLine()
格式二: BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); (数据量大,要求速度块时应用,需要import java.io.* ,记得throws IOException和关闭流)
-
Scanner 类的 next() 方法: 1、一定要读取到有效字符后才可以结束输入。 2、对输入有效字符之前遇到的空白,next() 方***自动将其去掉。 3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。 next() 不能得到带有空格的字符串。
-
Scanner 类的 nextLine() 方法: 1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。(因此可以被用来跳过回车符号) 2、可以获得空白。
-
Scanner 类的 nextInt() 方法: 1.返回值是int类型的,以有效数字后的空格作为两个输入的数据的间隔。 2.next() 和nextLine()返回类型都是String
-
遇到了一个输出巨大的题,sout会tle。。 需要用
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
for(int i = 0;i < n;i ++)
{
for(int j = 0;j < m;j ++)
{
out.write(dist[i][j] + " ");
}
out.write("\n");
}
out.flush();
做类似处理