用鍵盤輸入一個數,字符,字符串,數組 (用Java)


  使用Scanner或BufferedReader實現

1. 使用Scanner

下面一個例子是,利用 Scanner 實現從鍵盤讀入integer或float 型數據

import java.util.Scanner;

public class Test {
    public static void main(String args[]) {
        //使用Scanner類定義對象
        Scanner in = new Scanner(System.in);
        System.out.println("please input a float number");
        //接收float型數據  
        float a = in.nextFloat();
        System.out.println(a);
        System.out.println("please input a integer number");
        //接收int型數據 
        int b = in.nextInt();
        System.out.println(b);
        System.out.println("please input a String ");
        //接收字符串型數據 
        String c = in.nextLine();
        System.out.println(c);
    }
}

  在Java SE 6及以上,可以使用Scanner類取得用戶的輸入,Scanner類位於java.util包中,如果你要使用Scanner取得用戶輸入的話,要加上 import java.util.Scanner;這條語句import的功能是告訴編譯器,你將使用java.util包中的Scanner類。

  new是創建一個對象,程序中new的意思是創建了一個Scanner類的對象scan。但是在創建Scanner類的對象時,需要用System.in 作為它的參數,也可以將Scanner看作是System.in對象的支持者,System.in取得用戶輸入的內容后,交給Scanner來作一些處理。

Scanner類中提供了多個方法next():取得一個字符串;

  • nextLine() :獲取一行字符串;
  • nextInt():將取得的字符串轉換成int類型的整數;
  • nextFloat():將取得的字符串轉換成float型;
  • nextBoolean():將取得的字符串轉換成boolean型;

  用Scanner獲得用戶的輸入非常的方便,但是Scanner取得輸入的依據是空格符,包括空格鍵,Tab鍵和Enter鍵。當按下這其中的任一鍵時,Scanner就會返回下一個輸入,所以用nextLine()方法;

  Scanner類中next()與nextLine()方法的區別:next()與nextLine()區別很明確,next() 方法遇見第一個有效字符(不是空格和換行符)時,開始掃描,當遇見第一個分隔符或結束符(空格或換行符)時,結束掃描,獲取掃描到的內容,也就是說使用next()方法獲得的是不含空格和換行符的單個字符串。而使用nextLine()時,則可以掃描到一行內容並作為一個字符串而被獲取到。

  當然還有其他方法獲取字符串,看下面這個你熟悉不熟悉

2. 使用BufferedReader

先看一個例子:利用 BufferedReader實現從鍵盤讀入字符串並寫進文件test.txt中。

import java.io .*;

public class Test {
    public static void main(String[] args) throws IOException {
        BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter buff = new BufferedWriter(new FileWriter("test.txt"));
        String str = buf.readLine();
        while (!str.equals("exit")) {
            buff.write(str);
            buff.newLine();
            str = buf.readLine();
        }
        buf.close();
        buff.close();
    }
}

  其實在Java SE 1.4及以前的版本中,尚沒有提供Scanner方法,我們獲得輸入時也是使用BufferReader的.

  BufferedReader類位於java.io包中,所以要使用這個類,就要引入java.io這個包:import java.io.BufferedReader。

  使用BufferedReader對象的readLine()方法必須處理java.io.IOException異常(Exception).
使用BufferedReader來取得輸入,理解起來要復雜得多。但是使用這個方法是固定的,每次使用前先如法炮制就可以了。

BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String text = buffer.readLine();
readLine()方法會返回用戶在按下Enter鍵之前的所有字符輸入,不包括最后按下的Enter返回字符。

獲取字符串的方法

import java.io.BufferedReader;  
import java.io.IOException;  
import java.io.InputStreamReader;  
public class TestBufferedReader {  
    public static void main(String[] args) throws IOException {  
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));  
        System.out.println("請輸入一串字符串");  
        String test = buffer.readLine();  
        System.out.println("您輸入的字符串是:" + test);  
    }  
}

3. 用Scanner獲取數組

3.1 用Scanner.next()

  限制數組個數,可以自己設置字符串長度。

import java.util.Scanner;  

public class Test {  
    public static void main(String args[]) {  
        System.out.println("請輸入五個數:");
        Scanner in = new Scanner(System.in);
        int[] b=new int[5];
        for(int i=0;i<b.length;i++){
        b[i]=in.nextInt();
        System.out.println(""第"+(i+1)+"個數是"+b[i]");
    }  
}

  就是每獲取一個數,就用數組把它儲存起來。

3.2 用Scanner.nextLine()

  不限制數組個數。

import java.util.Scanner;  

public class Test {  
    public static void main(String args[])  {
        System.out.println("請輸入幾個數,用空格分開");
        Scanner scan = new Scanner(System.in);
        String str;
        str = scan.nextLine();
        String[] arr  = str.split(" ");
        int[] b = new int[arr.length];
        for(int j = 0; j<b.length;j++) {
            b[j] = Integer.parseInt(arr[j]);
            System.out.println("第"+(j+1)+"個數是"+b[j]);
        }
    }  
}

  通過字符串的形式,再轉化成int[ ] 數組。獲取一行,然后去掉空格,成了一個字符串,再把字符串分成若干個數。依次存入數組中。

其實這個用 BufferedReader 也可以實現,這里就不寫出了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM