关于java输入输出的总结
next()、nextLine()、nextInt()的区别和使用方法
next(),nextInt(),nextDouble()....:这些可以认为是一类,首先这些都是“阻塞式的”,意思就是如果遇到空格,Tab,Enter,会继续等待,直到获得相应类型相应的值!!!!!而且!!焦点不会移动到下一行,仍然处在这一行上。
next()---String, nextInt()----int, nextDouble()----double....
java输入hasNext()和hasNextLine()方法的区别:
在检查输入流时:
- 
hasNext()方法会判断接下来是否有非空字符.如果有,则返回true,否则返回false
- 
hasNextLine()方法会根据行匹配模式去判断接下来是否有一行(包括空行),如果有,则返回true,否则返回false
比如当前我们有如下测试用例: 7 15 9 5
这个测试用例在牛客网上是以文件的形式进行存储的. 而在 linux 系统中文件的结尾会有一个换行符\n,也就是说从System.in输入流中真正读取到的数据流是这样的:7 15 9 5\n 程序在处理完5之后,输入流中就只剩下一个换行符\n了,在处理完5之后while再去进行循环判断,此时hasNext()方法和hasNextLine()方法去判断得到的结果就产生了差异. hasNext()方法会认为之后再没有非空字符,会返回一个false hasNextLine()方法会认为换行符\n是一个空行,符合行的匹配模式,则会返回一个true,但实际上由于之后再没有数据了,所以会在读取输入流的时候发生异常,从而导致整个运行报错. 建议方案: 采用hasNextXxxx() 的话,后面也要用nextXxxx():比如前面用hasNextLine(),那么后面要用 nextLine() 来处理输入;后面用 nextInt() 方法的话,那么前面要使用 hasNext()方法去判断.
在线输入输出练习
1. 一直输入模板
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
        //操作
        }
    }
}
2. 有组数或者输入个数
import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n=sc.nextInt();
        while(n>0) {
 //操作
            n--;
        }
    }
}
3. 一行有多个信息 split切分
// a c bb 一直输入
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String[] strs = sc.nextLine().split(" ");//next()是从遇到第一个有效字符开始扫描,遇到第一个空格或换行符结束。nextLine()则是扫描剩下的所有字符串知道遇到回车为止。
         //操作
        }
    }
}
4.含结束标志
/**4. 数组求和   0为结束
 *
4 1 2 3 4(4个数 和为1+2+3+4 )
5 1 2 3 4 5
0
 *
10
15
 */
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();
            if (n == 0) {
                return;
            }
            int sum = 0;
            //while (n-- > 0){
            //    sum += input.nextInt();
            //}
            for (int i = 0; i < n; i++) {
                sum += sc.nextInt();
            }
            System.out.println(sum);
        }
    }
}
5. 小技巧 /函数
5.1 拼接
String[] strs = sc.nextLine().split(" ");
            Arrays.sort(strs);
            // System.out.println(String.join(" ",s));
            String res = "";
            for(String s : strs)
                res += s + " ";
            System.out.println(res.trim());
5.2 强转型 字符串数组转整数
// sc  0 0 0 0 0
String[] str =  sc.nextLine().split(" ");
            for(int i=0;i<str.length;i++){
                sum+=Integer.parseInt(str[i]);
            }
            //for(String str:int1){
            //    i+=Integer.valueOf(str);
            //}
6.next() 与 nextLine() 区别
next(): 1、一定要读取到有效字符后才可以结束输入。 2、对输入有效字符之前遇到的空白,next() 方法会自动将其去掉。 3、只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。 next() 不能得到带有空格的字符串。 nextLine(): 1、以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。 2、可以获得空白。
Windows中的Enter键相当于回车(\r)+换行(\n),分别完成将光标移到行首、移到下一行的功能; nextInt():直至读取到空格或回车之后结束本次的int值; next():直至读取到空格或回车之后结束本次的String值,不可读取回车; nextLine():直至读取到换行符(回车)之后结束本次读取的String,可读取回车(空值)。 nextInt()或者next()读取完毕并回车之后其后紧跟nextLine(),就会导致nextLine()读取到空值,因为nextLine()自动读取到'\n',意味着遇到结束符; 有时候将字符串转为整数时,代码没问题却提示数组越界,往往是因为字符串代表的整数超过了int的最值,需要改用long。 
 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号