怎么建立一个网站,深圳文化墙制作公司,如何建一个网站多少钱,汉服网页设计作品参考链接#xff1a; Java之Scanner类
最近在做OJ类问题的时候#xff0c;经常由于Scanner的使用造成一些细节问题导致程序不通过#xff08;最惨的就是网易笔试#xff0c;由于sc死循环了也没发现#xff0c;导致AC代码也不能通过。。。#xff09;#xff0c;因此对S…参考链接 Java之Scanner类
最近在做OJ类问题的时候经常由于Scanner的使用造成一些细节问题导致程序不通过最惨的就是网易笔试由于sc死循环了也没发现导致AC代码也不能通过。。。因此对Scanner进行了一些总结整理。我的githubhttps://github.com/MonkeyJJC?tabrepositories
Scanner类简介 Java 5添加了java.util.Scanner类这是一个用于扫描输入文本的新的实用程序。它是以前的StringTokenizer和Matcher类之间的某种结合。由于任何数据都必须通过同一模式的捕获组检索或通过使用一个索引来检索文本的各个部分。于是可以结合使用正则表达式和从输入流中检索特定类型数据项的方法。这样除了能使用正则表达式之外Scanner类还可以任意地对字符串和基本类型(如int和double)的数据进行分析。借助于Scanner可以针对任何要处理的文本内容编写自定义的语法分析器。
关于nextInt()、next()和nextLine()的理解
nextInt(): it only reads the int value, nextInt() places the cursor光标 in the same line after reading the input.nextInt()只读取数值剩下\n还没有读取并将cursor放在本行中
next(): read the input only till the space. It can’t read two words separated by space. Also, next() places the cursor in the same line after reading the input.next()只读空格之前的数据并且cursor指向本行 next() 方法遇见第一个有效字符非空格非换行符时开始扫描当遇见第一个分隔符或结束符(空格或换行符)时结束扫描获取扫描到的内容即获得第一个扫描到的不含空格、换行符的单个字符串。
nextLine(): reads input including space between the words (that is, it reads till the end of line \n). Once the input is read, nextLine() positions the cursor in the next line. nextLine()时则可以扫描到一行内容并作为一个字符串而被获取到。
public class NextTest{ public static void main(String[] args) { String s1,s2; Scanner scnew Scanner(System.in); System.out.print(请输入第一个字符串); s1sc.nextLine(); System.out.print(请输入第二个字符串); s2sc.next(); System.out.println(输入的字符串是s1 s2); }
} 结果
请输入第一个字符串home
请输入第二个字符串work
输入的字符串是home work 把上面的程序修改一下
s1sc.next();
s2sc.nextLine(); 运行结果
请输入第一个字符串home
请输入第二个字符串输入的字符串是home 可以看到nextLine()自动读取了被next()去掉的Enter作为他的结束符所以没办法给s2从键盘输入值。经过验证我发现其他的next的方法如double nextDouble() float nextFloat() int nextInt() 等与nextLine()连用时都存在这个问题解决的办法是在每一个 next()、nextDouble() 、 nextFloat()、nextInt() 等语句之后加一个nextLine()语句将被next()去掉的Enter结束符过滤掉。
public class NextTest{ public static void main(String[] args) { String s1,s2; Scanner scnew Scanner(System.in); System.out.print(请输入第一个字符串); s1sc.next(); sc.nextLine(); System.out.print(请输入第二个字符串); s2sc.nextLine(); System.out.println(输入的字符串是s1 s2); }
} 运行结果
请输入第一个字符串home
请输入第二个字符串work
输入的字符串是home work 循环输入多组测试用例
一个while就是一个测试用例 public static void main(String[] args){ Scanner in new Scanner(System.in); // 一个while就是一个测试用例 while(in.hasNext()){ int n in.nextInt(); // 该测试用例后续接收的参数个数 long[] array new long[n]; String[] arrayStr new String[n]; for(int i0; in; i){ arrayStr[i] in.next(); } for(int i0; in; i){ array[i] in.nextLong();// 取下一个元素转换成long类型 } System.out.println(Arrays.toString(array) Arrays.toString(arrayStr)); } } 一个与容器结合的综合例子
import java.util.Scanner;
public class Main { public static void main(String[] args) { Scanner in new Scanner(System.in); while (in.hasNext()) { int n in.nextInt(); /* nextLine()是扫描器执行当前行并返回跳过的输入信息特别需要注意 如果没有该行则执行第一个in.nextLine()命令时的返回值是int n in.nextInt()的值*/ in.nextLine(); HashSetString set new HashSetString(); for (int i 0; i n; i) { String line in.nextLine(); String[] arr line.split( ); for (int j 0; j arr.length; j) { set.add(arr[j]); } } System.out.println(sum: set.size()); }
} 输入 3 a b c d e f a b c 输出 6