Java中Scanner的常用方法總結(一次學懂)
Scanner類是一個可以幫助用戶鍵盤輸入內容的一個類。
一.基本的Scanner輸入
①輸入整數、小數
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int a = scanner.nextInt();
double b = scanner.nextDouble();
System.out.println(a);
System.out.println(b);
}
}運行截圖

②輸入一維數組
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();//定義數組長度為n
int[] arr = new int[n];//定義一個長度為n,名叫arr的數組
for (int i = 0; i < n; i++) {
arr[i]=scanner.nextInt();
}
System.out.print("Scanner輸入的數組為:");
for (int i = 0 ; i < n ; i++) {
System.out.print(arr[i]+" ");
}
}
}運行截圖

輸入二維數組
import java.util.Scanner;
public class Test2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[][] mat = new int[n][m];
System.out.println("請輸入整數型的二維數組:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mat[i][j]=scanner.nextInt();
}
}
for (int i = 0; i < n; i++) {
System.out.print("[");
for (int j = 0; j < m; j++) {
if(j==m-1) {
System.out.print(mat[i][j]);
}else {
System.out.print(mat[i][j]+",");
}
}
System.out.print("]");
System.out.println();
}
}
}運行截圖

③輸入字符串數組
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String[] arr = new String[n];
for(int i = 0 ; i < n ; i ++) {
arr[i]=scanner.next();
}
System.out.println("輸入的字符串數組為:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
}
}
}運行截圖

二.例【猜數字】
創(chuàng)建ScannerDemo類,首先在主方法中創(chuàng)建一個隨機數,然后再創(chuàng)建一個while循環(huán)不斷獲取用戶輸入的數字,讓用戶輸入的數字與隨機數比較,給出“大于”或“小于”的提示,直到用戶輸入的數字與隨機數相等才結束循環(huán)。
import java.util.Random;
import java.util.Scanner;
public class ScannerDemo {
public static void main(String[] args) {
Random r = new Random();
int num = r.nextInt(100);
int input = -1;
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("猜一猜隨機數是多少?");
input=scanner.nextInt();
if (input>num) {
System.out.println("你輸入的數字大了!");
}else if(input<num) {
System.out.println("你輸入的數字小了!");
}else if(input==num) {
break;
}else {
System.out.println("您的輸入有誤!");
}
}
System.out.println("恭喜你答對了!");
scanner.close();
}
}運行截圖

附:Scanner可以指定任意 符號,字符等作為分割符;
范例:
String s = "you are Beautiful!you are kind! you are smart!";
Scanner scanner = new Scanner(s);
scanner.useDelimiter("!");
while (scanner.hasNext())
System.out.println(scanner.next());
結果:

總結
到此這篇關于Java中Scanner的常用方法總結的文章就介紹到這了,更多相關Java Scanner常用方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot?ErrorPageFilter的實際應用詳解
這篇文章主要介紹了springboot?ErrorPageFilter的實際應用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
springboot?集成identityserver4身份驗證的過程解析
這篇文章主要介紹了springboot?集成identityserver4身份驗證的相關知識,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
RocketMQ生產者如何規(guī)避故障Broker方式詳解
這篇文章主要為大家介紹了RocketMQ生產者如何規(guī)避故障Broker方式詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

