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);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43818177/article/details/105638100