Java小知识

输入:

1 使用BufferedReader方法输入,这种方法很繁琐

1 //返回输入的一行数据
2     public static String readLine1() throws IOException
3     {
4         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
5         return br.readLine();
6     }


2 直接使用System.in输入

1 //获得输入的一数据块
2     public static String readLine2() throws IOException
3     {
4         byte buf[] = new byte[1024];
5         int i = System.in.read(buf);
6         return new String(buf, 0, i-2);        //有两个结束符,
,所以要减2
7     }


3使用Scanner输入,这种方法最好用

 1 /**
 2      * 可以使用s.next()输入一个不含空格的字符串,
 3      * s.nextInt():输入一个整数
 4      * s.nextDouble():输入一个double
 5      * s.nextByte():输入一个字符
 6      **/
 7     public static String readLine3()
 8     {
 9         Scanner s = new Scanner(System.in);
10         return s.nextLine();            //s.nextInt();
11     }

输出:
  输出指定位数的小数
    1 System.out.println(String.format("%.2f", f));//要n位就"%.nf" 
原文地址:https://www.cnblogs.com/tianxxl/p/8040350.html