java学习笔记之Math、Random、Integer、Calender类

猜数字游戏:

package com.mcq;

import java.util.Scanner;

public class MathDemo {
	public static void main(String[] args) {
		int num=(int)(Math.random()*100)+1,cnt=0;
		while(true){
			System.out.println("请输入一个1-100的数字");
			Scanner scanner=new Scanner(System.in);
			String next=scanner.next();
			int guess=-1;
			cnt++;
			try {
				 guess=Integer.valueOf(next);
			} catch (NumberFormatException e) {//数字转换异常
				// TODO: handle exception
				System.out.println("请输入数字!!!");
				continue;
			}
			if(guess<1||guess>100){
				System.out.println("输入的数字不在范围内");
//				throw new NumberException("数字不在范围内异常");
			}
			if(guess<num){
				System.out.println("你猜的小啦");
			}else if(guess>num){
				System.out.println("你猜的大啦");
			}else{
				System.out.println("你猜对啦 你猜了"+cnt+"次");
				break;
			}
		}
	}
}

Random类用法:

package com.mcq;

import java.util.Random;

public class RandomDemo {
	public static void main(String[] args) {
		Random random=new Random();
		System.out.println(random.nextInt(100));//均匀分布
	}
}

Integer:

package com.mcq;

public class IntegerDemo {
	public static void main(String[] args) {
		Integer i=1;//自动装箱Integer i=Integer.valueOf(1);
		int a=i;//自动拆箱int a=i.intValue();
	}
}

面试题:Integer num=0; num+=1;在这个过程中做了哪些操作?

答:先自动拆箱,后自动装箱。

BigDecimal

package com.mcq;

import java.math.BigDecimal;

public class BigDecimalDemo {
	public static void main(String[] args) {
		double a=0.01,b=0.09;
		BigDecimal aa=new BigDecimal("0.01"),bb=new BigDecimal("0.09");//传入的浮点数用字符串表示
		System.out.println(a+b);
		System.out.println(aa.add(bb));
	}
}

Calender

package com.mcq;

import java.util.Date;
import java.util.Calendar;

public class CalenderDemo {
	public static void main(String[] args) {
		Calendar calender=Calendar.getInstance();
		System.out.println(calender);
		
		System.out.println(new Date().getTime());
		
		System.out.println(calender.get(calender.YEAR));
		System.out.println(calender.get(calender.MONTH)+1);//月要加1
		System.out.println(calender.get(calender.DATE));
		calender.set(2000,2,30);//设置日期
		
		System.out.println(calender.get(calender.YEAR));
		System.out.println(calender.get(calender.MONTH)+1);//月要加1
		System.out.println(calender.get(calender.DATE));
	}
}

String

package com.mcq;

public class StringDemo {
	public static void main(String[] args) {
		String str = "12345";
		char[] arr = str.toCharArray();
		for (char c : arr) {
			System.out.println(c);
		}
		String s = String.valueOf(arr);
		String s1 = String.valueOf(true);
		System.out.println(s);
		System.out.println(s1);
		
		String s2="  12 5 36 8 ";
		System.out.println(s2.trim());//去除前后的空格
		String s3=s2.replace("12 ", "233");//新字符/字符串替换旧字符/字符串
		System.out.println(s3);
		System.out.println("--------------------");
		int num = 123;
		String string=String.valueOf(num);//int转String
		int a=Integer.valueOf(string);//String转int
		
		String string2="hehe";
		String string3="hehe";//String里相同的串只存在一个,所以string3的地址==string2的地址
		String string4=new String("hehe");
		System.out.println(string2==string3);//==是比较地址
		System.out.println(string2==string4);
		String string5=string2.concat("");//concat的参数如果是空串就返回原对象,否则新建一个对象
		String string6=string2+"";//+直接创建新的对象
		System.out.println(string2==string5);
		System.out.println(string2==string6);
		System.out.println(string2.equals(string5));
	}
}

/*
1
2
3
4
5
12345
true
12 5 36 8
  2335 36 8 
--------------------
true
false
true
false
true

*/

StringBuilder

package com.mcq;

public class StringBuilderDemo {
	public static void main(String[] args) {
		StringBuilder stringBuilder=new StringBuilder();//容量默认是16
		System.out.println(stringBuilder.capacity());
		StringBuilder stringBuilder2=new StringBuilder(1024);
		//链式编程
		StringBuilder append=stringBuilder2.append("hello").append(" world").append(new Student("小明",18));
		System.out.println(append);//实际上添加的是student的toString方法返回的字符串
		stringBuilder2.insert(5, "gg");//从第五个位置开始插入
		System.out.println(stringBuilder2);
		
		stringBuilder2.reverse();//反转
		System.out.println(stringBuilder2);
		
	}
}

/*
16
hello world小明 18
hellogg world小明 18
81 明小dlrow ggolleh
*/

效率:StringBuilder>StringBuffer>String

package com.mcq;

public class StringBuilderDemo2 {
	public static void main(String[] args) {
		int arr[]={1,2,3,4,5,6,7};
		long start=System.currentTimeMillis();
		for(int i=0;i<1000000;i++){
			
//			stringBuilder(arr);//386 379 389 ms
//			stringBuffer(arr);//953 955 664 ms
			str(arr);//1191 1072 1117 ms
		}
		long end=System.currentTimeMillis();
		System.out.println(end-start);
		
	}
	public static void stringBuilder(int []arr){
		StringBuilder sb=new StringBuilder("[");
		for(int i=0;i<arr.length;i++){
			if(i!=arr.length-1){
				sb.append(arr[i]).append(",");
			}else {
				sb.append(arr[i]).append("]");
			}
		}
//		System.out.println(sb);
	}
	public static void stringBuffer(int []arr) {
		StringBuffer sb=new StringBuffer("[");
		for(int i=0;i<arr.length;i++){
			if(i!=arr.length-1){
				sb.append(arr[i]).append(",");
			}else {
				sb.append(arr[i]).append("]");
			}
		}
//		System.out.println(sb);
	}
	public static void str(int []arr) {
		String string="[";
		for(int i=0;i<arr.length;i++){
			if(i!=arr.length-1){
				string+=arr[i]+",";
			}else {
				string+=arr[i]+"]";
			}
		}
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值