1. 程式人生 > 其它 >5.1.Java流程控制之使用者互動Scanner

5.1.Java流程控制之使用者互動Scanner

  • 之前我們學的基本語法中我們並沒有實現程式和人的互動,但是Java給我們提供了這樣一個工具類,我們可以獲取使用者的輸入。java.util.Scanner是 Java5的新特徵,我們可以通過Scanner類來獲取使用者的輸入。

  • 基本語法:

    Scanner s = new Scanner(System.in);
  • 通過Scanner類的next()與nextLine()方法獲取輸入的字串,在讀取前我們一般需要使用hasNext()與hasNextLine()判斷是否還有輸入的資料。

  • next():

    • 1、一定要讀取到有效字元後才可以結束輸入。

    • 2、對輸入有效字元之前遇到的空白,next()方法會自動將其去掉。

    • 3、只有輸入有效字元後才將其後面輸入的空白作為分隔符或者結束符。

    • 4、next()不能得到帶有空格的字串。

package com.lanseyutou.scanner;

import java.util.Scanner;

public class Dome01 {
public static void main(String[] args) {

//建立一個掃描器物件,用於接收鍵盤資料

Scanner scanner = new Scanner(System.in);

System.out.println("使用next方式接收:");

//判斷使用者沒有輸入字串
if(scanner.hasNext()){
//使用next接收
String str = scanner.next();
System.out.println("輸出的內容為:"+str);
}

//凡是屬於IO流的類如果不關閉會一直佔用資源
scanner.close();

}
}

/*
使用next方式接收:
Hello World
輸出的內容為:Hello
*/
  • nextLine():

    • 1、以Enter為結束符,也就是說nextLine()方法返回的是輸入回車之前的所有字元。

    • 2、可以獲得空白。

package com.lanseyutou.scanner;

import java.util.Scanner;

public class Dome02 {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("用nextLine方式接收:");

if (scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println("輸出的內容為:"+str);
}

scanner.close();

}

}

/*
用nextLine方式接收
Hello world!
輸出的內容為:Hello world!
*/
package com.lanseyutou.scanner;

import java.util.Scanner;

public class Demo03 {
public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("用nextLine方式接收:");

String str = scanner.nextLine();

System.out.println("輸出的內容為:"+str);

scanner.close();

}
}

/*
用nextLine方式接收:
藍色芋頭的乾飯生活 Java學習
輸出的內容為:藍色芋頭的乾飯生活 Java學習
*/
package com.lanseyutou.scanner;

import java.util.Scanner;

public class Dome04 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

//從鍵盤接收資料
int i = 0;
float f = 0.1F;

System.out.println("請輸入整數:");

if (scanner.hasNextInt()){
i = scanner.nextInt();
System.out.println("整數資料:"+i);
}else {
System.out.println("輸入的不是整數資料!");
}

System.out.println("請輸入小數:");

if (scanner.hasNextFloat()){
f = scanner.nextFloat();
System.out.println("小數資料:"+f);
}else {
System.out.println("輸入的不是小數資料!");
}
}
}

/*
請輸入整數:
10
整數資料:10
請輸入小數:
1.1
小數資料:1.1
*/

/*
請輸入整數:
10.1
輸入的不是整數資料!
請輸入小數:
小數資料:10.1
*/
package com.lanseyutou.scanner;

import java.util.Scanner;

public class Demo05 {
public static void main(String[] args) {
//我們要輸入多個數字,並求其總和與平均數,每輸入一個數字用回車確認,通過輸入非數字來結束輸入並輸出執行結果

Scanner scanner = new Scanner(System.in);

//和
double sum = 0;

//計算輸入了多少個數
int m = 0;
System.out.println("請輸入整數(輸入非整數結束輸入):");
//通過迴圈判斷是否還有輸入
while(scanner.hasNextDouble()){

double x = scanner.nextDouble();
m=m+1;
sum = sum + x;
System.out.println("你輸入了第"+ m + "個數據,當前結果為:"+sum);
}

System.out.println(m + "個數的和為:"+sum);
System.out.println(m + "個數的平均值為:"+(sum/m));

scanner.close();

}
}

/*
請輸入整數(輸入非整數結束輸入):
10
你輸入了第1個數據,當前結果為:10.0
20
你輸入了第2個數據,當前結果為:30.0
x
2個數的和為:30.0
2個數的平均值為:15.0
*/