public class switchstatement { public static void main (String[] args){
int testValue = 2; switch (testValue) { case (0-49) System.out.println("F"); break; case (>=50) System.out.println("D"); break; case (>=60) System.out.println("C"); break; case (>=70) System.out.println("B"); break; case (80-100) System.out.println("A"); break; } } }
ผมลองมาให้คุณและ ลองแล้ว eclipse มันจะแจ้ง error ในบรรทัดแรกของ case เนื่องจาก case มันใช้กับ ตัวเลข(ที่ไม่ใช่ กลุ่มข้อมูลเช่น 0-49 มันจะใช้กับ 0,1,2... เช่น case 0: case 1: case 2:)
คำสั่งที่ควรใช้มากกว่า switch...case คือ คำสั่ง if
public class Hellosourcecode {
public static void main(String[] args) { int testValue = 2;
if (testValue <= 49) { System.out.println("F"); } else if (testValue <= 59) { System.out.println("D"); } else if (testValue <= 69) { System.out.println("C"); } else if (testValue <= 79) { System.out.println("B"); } else if (testValue <= 100) { System.out.println("A"); } }