《程序员应聘在线编程如何使用Scanner》
Scanner的用法
某厂的笔试题,与常见的在线编程题不同的是,需要自己新建Main类,新建main()方法,并且输入的参数是以Scanner方式输入的,若是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"还没有读取,并将Scanner类读取依靠的cursor放在数值后面,"\n"前面,因此如果用nextInt()读取后,再用nextLine()读取,读取到的是换行符)
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()时,则可以扫描到一行内容并作为一个字符串而被获取到。如果要读取带空格的一串字符串还是需要使用nextLine(),而非next()方法。
如果使用next()、nextDouble() 、 nextFloat()、nextInt() 读取后,其实还有一个回车未读取出来,若此时后面跟上一个nextLine()读取的是该回车。碰到nextLine()若Scanner中确实没有东西可以读取,会等待你输入。
例如:讲道理在运行nextLine()时我想再次输入,但是它读取了next()剩下的回车。
当在sc.next()后面加一个sc.nextLine()即可消除该回车。
当你输入一串带空格的字符串,敲下回车后,若最开始碰到sc.next(),sc.next()只会读取第一个不是空格的单词。例如下例,当sc.next读完hello后,读取的cursor是在空格前,导致读取的nextLine长度是6。
循环时候如何使用hasNext()方法
遇到hasNext()时,Scanner也会阻塞,等待你输入,等你输入后返回true。查看jdkapi,你会发现该方法当Scanner缓存区中有值可读时,会返回true,若没有,会一直阻塞等待你输入。
所以下面需要你先输入,再显示“请输入”,不要感到意外,因为阻塞在hasNext()方法中。
当我们想退出用hasNext作条件的while()循环时,那么要么控制台手工输入ctrl+z,要么 while(!sc.hasNext("#"))约定当一行输入#时退出。
实际线上笔试题要使用hasNext循环才能通过多组测试例子
实际线上笔试题中,要采用hasNext循环应对多组数据测试数据。举个例子,华为厂的一道题,编完函数后测试发现case通过率0.98%,自我感觉在eclipse上运行测试没有问题。
点击进去查看输出,发现我的输出为空,突然顿悟,由于我程序中只是一条sc.nextLine(),当通过一条测试用例后,main方法结束,不会再继续接受测试用例。
改成循环后,成功。
以下是该题代码:
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map.Entry;;
public class Main{
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
while(scan.hasNext())
{
String string = scan.nextLine();
char [] inputs = string.toCharArray();
System.out.println(getResult(inputs));
}
}
public static int getResult(char[] chars)
{
int index = 0;
if(chars.length == 0) return 0;
if(chars.length == 1) return 1;
if(chars.length == 2)
{
if(chars[0] == chars[1]) return 2;
else return 0;
}
int max = 1,i = 0,j = 0,temp = 0;
for( i = 1;i < chars.length - 1;i++) //密码可能是奇数个
{
j = 1;
while(chars[i - j] == chars[i+j])
{
j++;
if(i - j<0 || i+j >= chars.length)
{
temp = j*2-1;
break;
}
}
temp = j*2-1;
if(temp>max)
{
max = temp;
index = i;
}
}
temp = 0;
for( i = 1;i < chars.length - 1;i++) //密码可能是偶数个
{
j = 0;
while(chars[i - j] == chars[i+1+j])
{
j++;
if(i - j<0 || i+1+j >= chars.length)
{
temp = j*2;
break;
}
}
temp = j*2;
if(temp>max)
{
max = temp;
index = i;
}
}
// System.out.println("最大密码中心为:"+index);
return max;
}
}
用hasNextInt()作为判断下一个输入是否为数字需要配合next()方法使用
hasNextInt()函数大体意思表示scanner当前的标记的输入是否为int,并不会自动的移动标记。
例如实现一个判断当前到输入是否为数字,不是的话提示输入数字。若是以下代码:当输入的不是数字时,会一直输出请输入数字。
public class TestScanner {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input = 0;
while(true){
if(scanner.hasNextInt()){
input = scanner.nextInt();
break;
}else{
System.out.println("请输入数字");
}
}
System.out.println("输入的数字是:"+input);
}
}
运行效果:
要实现hasNextInt的读取的标记位置的改变,可以用scanner.next()移动。
解决办法;