iT邦幫忙

1

從零開始學習Java筆記(基本語法)

  • 分享至 

  • xImage
  •  

Java 能力測試
📌 測驗 1:變數與資料型別
請問 Java 有哪些基本的資料型別?
請問下面這段程式碼有錯嗎?如果有,請說明錯誤的地方

int a = 10;
double b = 5.5;
boolean c = "true";

問題 1:Java 有哪些基本的資料型別?
Java 的基本資料型別(Primitive Types)分為 4 類 8 種:

整數類型:

byte(1 byte,範圍:-128 到 127)
short(2 bytes,範圍:-32,768 到 32,767)
int(4 bytes,範圍:-2^31 到 2^31-1)
long(8 bytes,範圍:-2^63 到 2^63-1,使用 L 表示長整數,如 100L)
浮點數類型:

float(4 bytes,使用 F 表示,如 3.14F)
double(8 bytes,預設的浮點數類型,如 3.14)
字元類型:

char(2 bytes,存 Unicode 字元,如 'A' 或 '\u0041')
布林類型:

boolean(1 bit,只有 true 或 false)
🔹 額外補充:

String 不是基本資料型別,而是物件(Class)。
null 不能作為基本型別的值,因為基本型別不存於 Heap,而是 Stack。

可以學習:
✅ 變數與資料型別的詳細說明
✅ 基本型別 vs. 物件型別的區別

📌 測驗 2:條件判斷(if-else / switch)
請寫一段程式,輸入一個數字,判斷它是奇數還是偶數
下面這段 switch 語法是否有錯?

int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
    case 2:
        System.out.println("Tuesday");
    case 3:
        System.out.println("Wednesday");
    default:
        System.out.println("Other day");
}

📌 測驗 3:迴圈(for / while)
請寫一段程式,計算 1 到 100 的總和
下面這段 while 迴圈是否有錯?如果有,該如何修正?

int i = 0;
while (i < 5);
    System.out.println("Hello");
    i++;

📌 測驗 4:方法(函式)
請寫一個方法 sum(int a, int b),回傳兩數相加的結果
請問 Java 方法的 return 有什麼限制?



📌 測驗 5:陣列
請寫一段程式,建立一個 int 陣列 {10, 20, 30, 40, 50},並印出所有元素
請問 Java 陣列的長度是否可以改變?為什麼?



📌 測驗 6:字串處理
請問 String 在 Java 中是基本型別還是物件?
請寫一段程式,將 "Hello World" 轉換成全大寫
請問 equals() 和 == 在比較字串時有什麼不同?



圖片
  直播研討會

1 則留言

1
念戀
iT邦新手 4 級 ‧ 2025-02-12 12:09:57
// 練習
// 使用IntelliJ IDEA Community Edition  提示
// Chatgpt 解惑
public class Main {
    public static void main(String[] args) {
        //測驗 1:變數與資料型別
        {
            System.out.println("測驗 1:變數與資料型別");
            int a = 10;
            System.out.println(a);
            double b = 5.5d; //可加d/D 可不加
            System.out.println(b);
            boolean c = true; //boolean 不用加引號 
            System.out.println(c);
        }
        //測驗 2:條件判斷(if-else / switch)
        {
            System.out.println("測驗 2:條件判斷(if-else / switch)");
            Scanner scanner = new Scanner(System.in);
            System.out.print("請輸入一個數字:");

            int number = scanner.nextInt();  // 讀取輸入的整數
            int result = number % 2;

            if (result == 0) {
                System.out.println("偶數");
            } else {
                System.out.println("奇數");
            }

            scanner.close();
        }
        
        //測驗 2:條件判斷(if-else / switch)
        {
            int day = 3;

            ///加入break;
            switch (day) {
                case 1:
                    System.out.println("Mon");
                    break;
                case 2:
                    System.out.println("Tus");
                    break;
                case 3:
                    System.out.println("Wen");
                    break;
                default:
                    System.out.println("other");
                    break;
            }
        }

        // 測驗 3:迴圈(for / while)
        {
            System.out.println("測驗 3:迴圈(for / while)");

            int sum = 0;
            for (int x = 1; x <= 100; x++) {
                sum = sum + x;
            }
            System.out.println("1+2+3...+99+100 = " + sum);

            int i = 0;
            while (i < 5) {
                System.out.println("Hello");
                i++;
            }
        }

        //測驗 4:方法(函式)
        {
            System.out.println(" 測驗 4:方法(函式)");
            int a = 10 ;
            int b = 100 ;
            System.out.println(sum(a, b));
        }
        //測驗 5:陣列
        {
            System.out.println("測驗 5:陣列");

            int[] intArray = new int[]{10, 20, 30, 40, 50};
            /*
                for (int i = 0; i < intArray.length; i++) {
                    System.out.println(intArray[i]);
                }
            */
            // 提示寫成這樣
            for (int num : intArray) {
                System.out.println(num);
            }

            try {
                //Java 陣列的長度是否可以改變?為什麼?
                //沒看到新增新元素的方法,強制加入則發生錯誤 Array index is out of bounds
                //宣告固定長度 則無法改變長度
                intArray[5] = 60;

            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println(e);
            }
        }

        //測驗 6:字串處理
        {
            System.out.println("測驗 6:字串處理");
            // package java.lang
            // public final class String
            String string = "Hello World"; //object 物件
            System.out.println("Hello World.toUpperCase() =>" + string.toUpperCase());

            // a,b 指向不同的記憶體位置
            String ra = new String("hello");
            System.out.println("記憶體位置ra:" + System.identityHashCode(ra));
            String rb = new String("hello");
            System.out.println("記憶體位置rb:" + System.identityHashCode(rb));
            System.out.println(ra == rb);//比較記憶體位置 false
            System.out.println(ra.equals(rb)); //比較表面值 true

            String a = "hello"; //a 指向記憶體位置為hello的地方
            String b = "hello"; //b 指向記憶體位置為hello的地方
            System.out.println("記憶體位置hash a:" + System.identityHashCode(a));
            System.out.println("記憶體位置hash b:" + System.identityHashCode(b));
            //always true
            System.out.println(a == b);//比較記憶體位置
            System.out.println(a.equals(b)); //比較表面值
        }

    }
    //提示: Non-static method 'sum(int, int)' cannot be referenced from a static context
    //所以 :加入 static
    private  static int sum(int a, int b) {
        return a + b;
    }
}

感謝。
我這個還會再做修改,我習慣弄一點就先貼上來。

我要留言

立即登入留言