欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在Java中Scanner的用法總結(jié)

 更新時(shí)間:2021年10月30日 14:50:55   作者:Megustas_JJC  
這篇文章主要介紹了在Java中Scanner的用法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

最近在做OJ類問(wèn)題的時(shí)候,經(jīng)常由于Scanner的使用造成一些細(xì)節(jié)問(wèn)題導(dǎo)致程序不通過(guò)(最慘的就是網(wǎng)易筆試,由于sc死循環(huán)了也沒(méi)發(fā)現(xiàn),導(dǎo)致AC代碼也不能通過(guò)。。。),因此對(duì)Scanner進(jìn)行了一些總結(jié)整理。

Scanner類簡(jiǎn)介

Java 5添加了java.util.Scanner類,這是一個(gè)用于掃描輸入文本的新的實(shí)用程序。

它是以前的StringTokenizer和Matcher類之間的某種結(jié)合。由于任何數(shù)據(jù)都必須通過(guò)同一模式的捕獲組檢索或通過(guò)使用一個(gè)索引來(lái)檢索文本的各個(gè)部分。

于是可以結(jié)合使用正則表達(dá)式和從輸入流中檢索特定類型數(shù)據(jù)項(xiàng)的方法。這樣,除了能使用正則表達(dá)式之外,Scanner類還可以任意地對(duì)字符串和基本類型(如int和double)的數(shù)據(jù)進(jìn)行分析。

借助于Scanner,可以針對(duì)任何要處理的文本內(nèi)容編寫自定義的語(yǔ)法分析器。

關(guān)于nextInt()、next()和nextLine()的理解

nextInt(): it only reads the int value, nextInt() places the cursor(光標(biāo)) in the same line after reading the input.(nextInt()只讀取數(shù)值,剩下"\n"還沒(méi)有讀取,并將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()只讀空格之前的數(shù)據(jù),并且cursor指向本行)

next() 方法遇見(jiàn)第一個(gè)有效字符(非空格,非換行符)時(shí),開(kāi)始掃描,當(dāng)遇見(jiàn)第一個(gè)分隔符或結(jié)束符(空格或換行符)時(shí),結(jié)束掃描,獲取掃描到的內(nèi)容,即獲得第一個(gè)掃描到的不含空格、換行符的單個(gè)字符串。

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()時(shí),則可以掃描到一行內(nèi)容并作為一個(gè)字符串而被獲取到。

public class NextTest{  
    public static void main(String[] args) {  
        String s1,s2;  
        Scanner sc=new Scanner(System.in);  
        System.out.print("請(qǐng)輸入第一個(gè)字符串:");  
        s1=sc.nextLine();  
        System.out.print("請(qǐng)輸入第二個(gè)字符串:");  
        s2=sc.next();  
        System.out.println("輸入的字符串是:"+s1+" "+s2);  
    }  
}  

結(jié)果:

請(qǐng)輸入第一個(gè)字符串:home
請(qǐng)輸入第二個(gè)字符串:work
輸入的字符串是:home work

把上面的程序修改一下:

s1=sc.next();  
s2=sc.nextLine();  

運(yùn)行結(jié)果:

請(qǐng)輸入第一個(gè)字符串:home
請(qǐng)輸入第二個(gè)字符串:輸入的字符串是:home

可以看到,nextLine()自動(dòng)讀取了被next()去掉的Enter作為他的結(jié)束符,所以沒(méi)辦法給s2從鍵盤輸入值。

經(jīng)過(guò)驗(yàn)證,我發(fā)現(xiàn)其他的next的方法,如double nextDouble() , float nextFloat() , int nextInt() 等與nextLine()連用時(shí)都存在這個(gè)問(wèn)題,解決的辦法是:在每一個(gè) next()、nextDouble() 、 nextFloat()、nextInt() 等語(yǔ)句之后加一個(gè)nextLine()語(yǔ)句,將被next()去掉的Enter結(jié)束符過(guò)濾掉。

public class NextTest{  
    public static void main(String[] args) {  
        String s1,s2;  
        Scanner sc=new Scanner(System.in);  
        System.out.print("請(qǐng)輸入第一個(gè)字符串:");  
        s1=sc.next();  
        sc.nextLine();
        System.out.print("請(qǐng)輸入第二個(gè)字符串:");  
        s2=sc.nextLine();  
        System.out.println("輸入的字符串是:"+s1+" "+s2);  
    }  
}  

運(yùn)行結(jié)果:

請(qǐng)輸入第一個(gè)字符串:home
請(qǐng)輸入第二個(gè)字符串:work
輸入的字符串是:home work

循環(huán)輸入多組測(cè)試用例

一個(gè)while就是一個(gè)測(cè)試用例

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        // 一個(gè)while就是一個(gè)測(cè)試用例
        while(in.hasNext()){
            int n = in.nextInt(); // 該測(cè)試用例后續(xù)接收的參數(shù)個(gè)數(shù)
            long[] array = new long[n];
            String[] arrayStr = new String[n];
            for(int i=0; i<n; i++){
                arrayStr[i] = in.next();
            }
            for(int i=0; i<n; i++){
                array[i] = in.nextLong();// 取下一個(gè)元素轉(zhuǎn)換成long類型
            }
            System.out.println(Arrays.toString(array)+" "+ Arrays.toString(arrayStr));
        }
    }

一個(gè)與容器結(jié)合的綜合例子:

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()是掃描器執(zhí)行當(dāng)前行,并返回跳過(guò)的輸入信息,特別需要注意!?。?
 
            如果沒(méi)有該行,則執(zhí)行第一個(gè)in.nextLine()命令時(shí)的返回值是int n = in.nextInt()的值*/   
            in.nextLine();  
        HashSet<String> set = new HashSet<String>();  
        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

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論