java常见内置对象

1.Math类

Math类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。

static double E :比任何其他值都更接近 e (即自然对数的底数)的 double 值。

static double PI : 比任何其他值都更接近 pi (即圆的周长与直径之比)的 double 值。

static double ceil(double a) :返回最小的(最接近负无穷大) double 值,该值大于等于参数,并等于某个整数。

static double floor(double a) :返回最大的(最接近正无穷大) double 值,该值小于等于参数,并等于某个整数。

static double sqrt(double a) :返回正确舍入的 double 值的正平方根。
static double abs(double a) :返回 double 值的绝对值。
static double max(double a, double b) :返回两个 double 值中较大的一个。
static double min(double a, double b) :返回两个 double 值中较小的一个。
static double random() :返回带正号的 double 值,该值大于等于 0.0 且小于 1.0 。
代码实例:

	public static void main(String[] args) {
		System.out.println(Math.PI);
		System.out.println(Math.E);
		System.out.println(Math.abs(-3));
		System.out.println(Math.cbrt(27));
		System.out.println(Math.ceil(-3.3));
		System.out.println(Math.floor(-3.3));
		System.out.println(Math.pow(2,3));
		System.out.println(Math.random());
	}

运行结果:

2. Random类(随机数)

Random类是java.until下的一个根据随机算法的起源数字进行一些变化,从而得到随机数字的方法。

随机算法的起源数字被成为种子数(seed)。

虽然Random类产生的数字是随机的,但在相同种子数(seed)下的相同次数产生的随机数是相同的(伪随机)。

Random对象的生成

1、public Random()

此构造方法是以系统自身的时间为种子数来构造Random对象。

2、public Random(long seed)

此构造方法可以自己来选定具体的种子来构造Random对象

	public static void main(String[] args) {
	        Random r = new Random();//以系统自身时间为种子数
	        int i = r.nextInt();
	        System.out.println("i="+i);
	        Scanner sc  =new Scanner(System.in);
	        int j = sc.nextInt();
	        Random r2 = new Random(j);//自定义种子数
	        Random r3 = new Random(j);//这里是为了验证上方的注意事项:Random类是伪随机,相同种子数相同次数产生的随机数相同
	        int num  = r2.nextInt();
	        int num2 = r3.nextInt();
	        System.out.println("num"+num);
	        System.out.println("num2"+num2);
	    }

 运行结果:

 常用方法

  • random.nextInt()    返回值为整数,范围是int类型范围
  • random.nextInt(int n)    返回值为整数,该值介于[0,n)的区间
  • random.nextLong()    返回值为长整型,范围是long类型的范围
  • random.nextFloat()    返回值为小数,范围是[0,0.1]
  • random.nextDouble()    返回值为小数,范围是[0,0.1]
  • random.nextBoolean()    返回值为boolean值,true和false概率相同
	public static void main(String[] args) {
	        Random r = new Random();
	        int a=r.nextInt();
	        int b=r.nextInt(10);
	        long c=r.nextLong();
	        float d=r.nextFloat();
	        double e=r.nextDouble();
	        boolean f=r.nextBoolean();
	        System.out.println("a="+a);
	        System.out.println("b="+b);
	        System.out.println("c="+c);
	        System.out.println("d="+d);
	        System.out.println("e="+e);
	        System.out.println("f="+f);
	    }

运行结果: 

例:猜拳游戏,石头剪刀布。 随机数生成石头剪刀布(0:石头  1:剪刀  2:布)

public static void main(String[] args) {
	        Random machine = new Random();
	        Scanner sc = new Scanner(System.in);
	        System.out.println("欢迎参加猜拳游戏,请出拳");
	        System.out.println("0,石头,1,布,2,剪刀");
	        System.out.println("请输入数字选择猜拳");
	 
	        while (true) {
	            int player = sc.nextInt();//玩家出拳
	            int windows = machine.nextInt(3);//电脑出拳
	            if(player == 0){
	                System.out.println("您出了石头");
	            }else if(player == 1) {
	                System.out.println("您出了布");
	            }else if(player == 2) {
	                System.out.println("您出了剪刀");
	            }
	        
	            if(windows == 0){
	                System.out.println("电脑出了石头");
	            }else if(windows == 1) {
	                System.out.println("电脑出了布");
	            }else if(windows == 2) {
	                System.out.println("电脑出了剪刀");
	            }
	 
	            if (player == 0 && windows == 1 || player == 1 && windows == 2 || player == 2 && windows == 0) {
	                System.out.println("抱歉,您输了");
	            } else if (windows == 0 && player == 1 || windows == 1 && player == 2 || windows == 2 && player == 0) {
	                System.out.println("恭喜,您赢了");
	            } else if(player == 0 && windows == 0 || player == 1 && windows == 1 || player == 2 && windows == 2){
	                System.out.println("平局,再来一次");
	            }else{
	                System.out.println("请遵守游戏出拳规则");
	            }
	        }
	    }

3.Scanner对象

Java 5添加了java.util.Scanner类,这是一个用于扫描输入文本的新的实用程序。
常用于控制台的输入,当需要使用控制台输入时即可调用这个类

使用前提 

import java.util.*;

或者import java.util.Scanner;

常用的Scanner函数 

  • nextInt():输入的是数字,以返回作为结束,其中不能带有空格
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt();
		System.out.println(a);
	    }
输入 123
输出 123

next():只读取输入直到空格。它不能读两个由空格或符号隔开的单词。此外,next()在读取输入后将光标放在同一行中。(next()只读空格之前的数据,并且光标指向本行)

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s1=sc.next();
		System.out.println(s1);
	    }

输入ads dd
输出ads
  • nextLine():以Enter为结束符,也就是说 nextLine()方法返回的是输入回车之前的所有字符。可以获得空白。
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		String s2=sc.nextLine();
		System.out.println(s2);
	    }
输入xcd fgx
输出xcd fgx

 4. String类(字符串对象)

见:字符串初识

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值