if(expression1) { statement; } else if(expression2) { statement; } else if(expression3) { statement; } else { statement; }
package com.java.myapp; public class MyClass { public static void main(String[] args) { int i = 2; if(i == 1){ System.out.println("true statement i = 1"); }else if(i == 2){ System.out.println("true statement i = 2"); }else if(i == 3){ System.out.println("true statement i = 3"); }else if(i == 4){ System.out.println("true statement i = 4"); }else{ System.out.println("false statement i = " + i); } } }
package com.java.myapp; public class MyClass { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท