Java提升學習(五):Scanner類、Random類、Math類

Java提升學習(五):Scanner類、Random類、Math類

一、Scanner類
一個可以解析基本類型和字符串的簡單文本掃描器。 簡單來說就是從鍵盤收入輸入的數據。

  • 引用包:
import 包名.類名;
import java.util.Scanner;
  • 構造方法:public Scanner(InputStream source) :構造一個新的 Scanner ,它生成的值是從指定的輸入流掃描的。

  • 示例:

import java.util.Scanner;//這裏引用了輸入包

public class great {	
	
	public static void main(String[] args){//這是一個主方法
		
		Scanner a = new Scanner(System.in);
		int b = a.nextInt();
		System.out.print(b);
	}
}
-----------輸入----------
123
-----------輸出----------
123
  • 練習:
    求鍵盤輸入的三位數的最大數。
import java.util.Scanner;//這裏引用了輸入包

public class great {
	
	public static int max_num(int a , int b , int c) {
		
		int num = (a > b ? a :b);
		int num1 = (num > c ? num : c) ;
		
		return num1;	
	}
	
	
	public static void main(String[] args){//這是一個主方法
		
		System.out.println("input first number :");
		Scanner a = new Scanner(System.in);
		int a1 = a.nextInt();
		
		System.out.println("input second number :");
		Scanner b = new Scanner(System.in);
		int b1 = a.nextInt();
		
		System.out.println("input third number :");
		Scanner c = new Scanner(System.in);
		int c1 = a.nextInt();
		
		System.out.println("max_number is :" + max_num(a1 , b1 , c1));
	}
}

值得注意的是:這裏運用了匿名對象

new 類名(參數列表)
new Scanner(System.in)

示例:

import java.util.Scanner;//這裏引用了輸入包

public class great {
	
	public static String input(Scanner sth) {
		
		return sth.next();
	}
	
	
	public static void main(String[] args){//這是一個主方法
			
		System.out.println(input(new Scanner(System.in)));
	}
}
---------輸入---------
qwer123
---------輸出---------
qwer123

二、Random類

  • 導入:java.util.Random 該類需要 import導入使後使用。
  • 重塑構造方法: public Random() 創建一個新的隨機數生成器。
  • 查看成員:public int nextInt(int n) 返回一個僞隨機數,範圍在 0 (包括)和 指定值 n (不包括)之間的 int 值。
import java.util.Random; 
public class Demo01_Random {
    public static void main(String[] args) {
                //2. 創建鍵盤錄入數據的對象         
                Random r = new Random();           
                for(int i = 0; i < 3; i++){             
                //3. 隨機生成一個數據
                             int number = r.nextInt(10);             
                //4. 輸出數據 
                         	 System.out.println("number:"+ number);         } 
               } 
 }
  • 練習:
    猜數
import java.util.Scanner;//這裏引用了輸入包
import java.util.Random;

public class great {
	
	public static int input(Scanner sth) {
		
		return sth.nextInt();
	}
	
	public static void main(String[] args){//這是一個主方法
			
		Random a = new Random();
		int num = a.nextInt(10);
		
		System.out.print("Guess a number:");
		while (true){
			
			if(num == input(new Scanner(System.in))) {
				System.out.println("you got it:");
			}else {
				System.out.println("guess it again:");
			}
		}
	}
}

三、Math類
java.lang.Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。類似這樣的工具 類,其所有方法均爲靜態方法,並且不會創建對象,調用起來非常簡單。

  • 示例:直接調用方法
import java.lang.Math;

public class great {
	

	public static void main(String[] args){//這是一個主方法
		int a = Math.abs(-123);
		System.out.print(a);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章