目標:
掌握包裝類別的作用
掌握包裝類別的自動裝箱及自動拆箱的操作
掌握包裝類別的轉換操作
mingyilai 發表在 痞客邦 留言(0) 人氣(999)
在Java中一切的關係都是以繼承的關係存在,如果一個類在定義時沒有明確指名繼承那個類,則默認繼承Object類,實際上定義格式如下:
class Person extends Object{}
mingyilai 發表在 痞客邦 留言(0) 人氣(1,530)

分析:
寵物資訊自行設計,簡單設計出名字、顏色、年齡3個屬性。
寵物類別很多,如貓、狗都屬於寵物,所以寵物是一個標準。
只要符合此寵物標準都能放進寵物商店。
要儲存多種寵物就是一個物件陣列,如果寵物個數由使用者決定,則在建立時就要分配好能儲存的寵物個數。
mingyilai 發表在 痞客邦 留言(0) 人氣(61)
//在排序好的陣列中增加一個數字,增加後的數字插入到陣列合適的位置
int score[] = {23,54,83,24,56,99,11}; //宣告陣列,不用排列大小
for (int x = 1; x < score.length; x++) //判斷迴圈
{
for (int y = 0 ; y < score.length; y++)
{
if (score[x] < score[y]) //數字從小到大排列
{
int temp = score[x];
score[x] = score[y];
score[y] = temp;
}
}
}
for (int i = 0; i < score.length; i++)
{
System.out.print(score[i] + "\t");
}
}
mingyilai 發表在 痞客邦 留言(0) 人氣(4,094)
//直角在右上
int i,j,k;
int n = 5;//輸入要的層
for(i = 1 ; i<=n ; i++)//層數的for迴圈
{
for( j = 1 ; j <=i ; j++)//根據外層行號,輸出星號左邊空格
System.out.print(" ");
for(k = 1 ; k <=n-i+1 ; k++)//根據外層行號,輸出星號個數
System.out.print("*");
System.out.println();
}
mingyilai 發表在 痞客邦 留言(0) 人氣(25,934)
package testdegree;
public class TestDegree {
mingyilai 發表在 痞客邦 留言(0) 人氣(2,456)
package testbmi;
public class TestBMI {
public static void main(String[] args) {
java.util.Scanner scan = new java.util.Scanner(System.in);
//重複迴圈結構
do{
//取得輸入資料
System.out.print("輸入你的體重(公斤):");
double weight = scan.nextDouble(); //公斤
System.out.print("輸入你的身高(公分):");
double height = scan.nextDouble(); //
//公分 換算為公尺
height = height / 100;
//計算BMI= (體重KG/(身高m*身高m)
double bmi = weight / (height*height);
System.out.printf("體重:%2f 公斤 身高:%2f 公尺\n ", weight,height);
System.out.printf("YourBMI:%.1f", bmi);
//多條件選擇結構
if(bmi<18.5){
System.out.println("體重輕盈,可多吃點!");
}else if(bmi>18.5 && bmi<=23){
System.out.println("好身材,保持下去喔!");
}else if(bmi>23 && bmi<=27){
System.out.println("有稍微胖,多運動喔!");
}else if(bmi>27){
System.out.println("心寬體胖,少吃點吧!");
javax.swing.JOptionPane.showMessageDialog(null,"Hi\nwhich sport do you like?");
}else{
System.out.println("資料錯誤,重新輸入?");
}
double perfect = height*height*22;
System.out.println("你的理想體重:" + (int)perfect + "公斤");
System.out.println("---------------");
}while(true);
}
}
mingyilai 發表在 痞客邦 留言(0) 人氣(8,149)