裝箱、拆箱以及Math類

import java.util.Random;
import java.util.Scanner;


public class IntegerTest {
public static void main(String[] args) {
/*裝箱:把基本數據類型轉換爲對應對象類型
*作用:1、在需要用到對象類型的時候,可以轉換成對應的對象類型(集合裏面)
*  2、轉換成對象以後,擁有相應的屬性和方法,方便進行主句操作
*/
//方法1
/*int i = 5;
System.out.println(i);*/
/* Integer integer = new Integer(12);//裝箱,數據產生對應的屬性和方法
System.out.println("裝箱"+integer);
int i = integer;
System.out.println("拆箱"+i);
//方法二
Integer integer2 = new Integer("15");//引號裏面只能是純數字
System.out.println("裝箱"+integer2);
int j = integer2;
System.out.println("拆箱"+j);
//方法三
Integer integer3 = Integer.valueOf(21);//裝箱
System.out.println(integer3);
int k = integer3.intValue();//拆箱
System.out.println(k);
//方法四
Integer integer4 = 66;//裝箱
int l = integer4;//拆箱
System.out.println(l);



//字符串和基本數據類型的轉換
//字符串轉換成基本數據類型
int i1 = Integer.parseInt("123");
System.out.println(i1);
float f = Float.parseFloat("123");
System.out.println(f);
double d = Double.parseDouble("123");
System.out.println(d);
boolean b = Boolean.parseBoolean("true");
System.out.println(b);


//基本數據類型轉換成字符串類型
int i2 = 123;
String s = Integer.toString(i2);
System.out.println(s);
String s2 = 123+"";
System.out.println(s2);
String s3 = i2+"";
System.out.println(s3);
String s4 = '男'+"";
System.out.println(s4);
//Math類
//輸出最大值
int num1 = Math.max(20,25);
System.out.println(num1);
//輸出最小值
int num2 = Math.min(20,25);
System.out.println(num2);
//輸出3的平方(前面數的後面數次方)
double num3 = Math.pow(3, 2);
System.out.println(num3);
//開根號
double num4 = Math.sqrt(16);
System.out.println(num4);
//abs(求絕對值)
int i6 = -6;
int abs = Math.abs(i6);
System.out.println(abs);
//返回三角正弦值
double angle = (Math.PI)/2;
double sin = Math.sin(angle);
System.out.println(sin);
//返回三角餘弦值
double angle1 = (Math.PI)/6;
double cos = Math.cos(angle1);
System.out.println(cos);
//正切
double angle2 = (Math.PI)/4;
double tan = Math.tan(angle2);
System.out.println(tan);
//正切
double angle3 = (Math.PI)/4;
double asin = Math.asin(angle3);
System.out.println(asin);*/
/*
//random  返回一個從0到1的隨機數         包括0 ,不包括1。
//輸出一個從1到10 的隨機數
int random = (int)(Math.random()*10+1);
System.out.println(random);

//隨機輸出0或1
int money = Math.random()>=0.5?1:0;
System.out.println(money);

//隨機輸出5-10之間的數
int check = (int)(Math.random()*6+5);
System.out.println(check);
*/
/*
//計算半徑3.5的圓面積
System.out.println("計算半徑3.5的圓面積");
double r = 3.5;
System.out.println( Math.pow(r,2)*Math.PI);
//隨機輸出5個1-6的整數
System.out.println("隨機輸出5個1-6的整數");
for(int a = 0;a<5;a++){
System.out.print((int)(Math.random()*6+1)+"\t");
}
System.out.println();
//找出1-10之間的兩個隨機數,並輸出最小的
System.out.println("找出1-10之間的兩個隨機數,並輸出最小的");
int random1 = (int)(Math.random()*10+1);
int random2 = (int)(Math.random()*10+1);
System.out.println(random1+"  "+random2);
System.out.println(Math.min(random1, random2) );
*/
/*
String result = String.valueOf(1);
System.out.println(result);

String nums = "abc";
byte[] bytes = nums.getBytes();
System.out.println(Arrays.toString(bytes));
*/
/*
//將O替換爲0,將i替換爲1
String str = "272O50i66";
str = str.replace('O', '0');
str = str.replace('i', '1');
System.out.println(str);
String str1 = "My_English_NaMe";
String[] str2 = str1.split("_");
str2[0] = str2[0].toLowerCase();
str2[1] = str2[1].toUpperCase();
str2[2] = str2[2].toUpperCase();
str1 = str2[0]+str2[1]+str2[2];
System.out.println(str1);
*/
/*
Scanner input = new Scanner(System.in);
StringBuffer sb = new StringBuffer();
String word;
do{
System.out.println("請輸入單詞:");
word = input.next();
if(word.equals("end")){
System.out.println("結束輸入!");
break;
}else{
StringBuffer sb1 =sb.append(word+",");
sb = sb1;
}
}while(!word.equals("end"));
System.out.println(sb);
String result = sb.toString();
String[] result1 = result.split(",");
for(int i = 0;i<result1.length;i++){
System.out.println(result1[i]);
}
input.close();
*/
//Random
Random random = new Random();
int i = random.nextInt(26);
System.out.println(i);
long i2 = random.nextLong();
System.out.println(i2);
System.out.println("輸出10個隨機boolean類型數,求各佔比率");
double num = 0; 
for(int j = 0;j < 10; j++){
boolean boo = random.nextBoolean();
if(boo){
num ++;
}
System.out.print(boo+"\t");
}
System.out.println();
System.out.println(num/10);
System.out.println("輸出10個1-10之間隨機double類型數");
System.out.println();
for(int j = 0;j < 10; j++){
System.out.println(random.nextDouble()*9+1);
}
System.out.println("兩位隨機整數");
System.out.println((int)(random.nextDouble()*90+10));
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章