if (remainder == 10) { hexDigit = "A"; } else if (remainder == 11) { hexDigit = "B"; } else if (remainder == 12) { hexDigit = "C"; } else if (remainder == 13) { hexDigit = "D"; } else if (remainder == 14) { hexDigit = "E"; } else if (remainder == 15) { hexDigit = "F"; } else { hexDigit = "Error"; } |
switch (remainder) { case 10: hexDigit = "A"; break; case 11: hexDigit = "B"; break; case 12: hexDigit = "C"; break; case 13: hexDigit = "D"; break; case 14: hexDigit = "E"; break; case 15: hexDigit = "F"; break; default: hexDigit = "Error"; } |
A few things to note about switch statements:
switch (remainder) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: hexDigit = "" + remainder; break; case 10: hexDigit = "A"; break; case ... ... the rest is as above }
System.out.println("Enter size box you want"); int boxSize = scan.nextInt(); double price; if (boxSize == 10) { System.out.println("Small"); price = 2.5; } else if (boxSize == 20) { System.out.println("Medium"); price = 3.75; } else if (boxSize == 30) { System.out.println("Large"); price = 5.0; } else { System.out.println ("Not a valid size."); price = -1; }
! (age >= 65 && income < 20000)
if ( _______________________________________________________ ) System.out.println ("Your age is divisible by 5."); else System.out.println ("Your age is not divisible by 5.");