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(guessnum){
System.out.println("你猜的大啦");
}else{
System.out.println("你猜对啦 你猜了"+cnt+"次");
break;
}
}
}
}
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));//均匀分布
}
}
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;在这个过程中做了哪些操作?
答:先自动拆箱,后自动装箱。
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));
}
}
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));
}
}
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
*/
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
*/
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