範例程式 | 第一輸入(x值) | 第二輸入(y值) | 輸出 |
import java.util.Scanner; public class example{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int x = sc.nextInt(); int y = sc.nextInt(); if(x<y){ System.out.print("x比y小"); } else if(x>y){ System.out.print("x比y大"); } else{ System.out.print("x等於y"); } sc.close(); } } |
3 | 5 | x比y小 |
5 | 3 | x比y大 | |
4 | 4 | x等於y |
if(a>b){ System.out.print("a>b"); } |
等同 |
if(a>b) System.out.print("a>b"); |
範例程式 | 輸入(x值) | 輸出 |
import java.util.Scanner; public class example{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int x = sc.nextInt(); switch(x){ case 0: System.out.print("x is 0"); break; case 1: System.out.print("x is 1"); break; case 2: System.out.print("x is 2"); break; case 3: System.out.print("x is 3"); break; } sc.close(); } } |
0 | x is 0 |
1 | x is 1 | |
2 | x is 2 | |
3 | x is 3 |