if (condition) { statement1 statement2 ... } ///if else if (condition) { statement1 statement2 ... } else { statement1 statement2 ... } //else if import java.util.Scanner; public class ElseIf { public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Enter a grade: "); int grade = in.nextInt(); if (grade < 60) { System.out.println('F'); } else if (grade < 70) { System.out.println('D'); } else if (grade < 80) { System.out.println('C'); } else if (grade < 90) { System.out.println('B'); } else { System.out.println('A'); } } } public class DayPrinter { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Enter an integer: "); int dayNum = in.nextInt(); String day; if (dayNum == 0) { day = "Sunday"; } else if (dayNum == 1){ day = "Monday"; } else if (dayNum == 2){ day = "Tuesday"; } else if (dayNum == 3){ day = "Wednesday"; } else if (dayNum == 4){ day = "Thursday"; } else if (dayNum == 5){ day = "Friday"; } else if (dayNum == 6){ day = "Saturday"; } else { day = "Int does not correspond to a day of the week"; } System.out.println(day); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter