Flowchart of FIG. 1-1 if ... else if ... else statement
Selection structure if statement ternary operator Conversion
Ternary operator, and it is similar to if-else statement syntax is as follows:
To determine the conditional expression 1:? 2 expression
Ternary operator will get a result, typically used for a variable assignment, when the judgment condition is satisfied, the expression that evaluates to a value , or the result of expression2 value .
java.util.Scanner Import; class Demo03 { public static void main (String [] args) { Scanner Scanner new new SC = (the System.in); System.out.println ( "Enter first integer"); int num1 sc.nextInt = (); System.out.println ( "Please enter the second integer"); int num2 = sc.nextInt (); int max; IF (num1> num2) { max = num1; } the else { max num2 =; } System.out.println ( "maximum value:" + max); } }
java.util.Scanner Import; Import java.util.Random; class Demo04 { public static void main (String [] args) { Scanner Scanner new new SC = (the System.in); .. 1 // input three numbers so that, with ternary operator selecting the maximum value System.out.println ( "enter first integer"); int num1 = sc.nextInt (); System.out.println ( "Please enter the second integer"); int num2 sc.nextInt = (); System.out.println ( "Please enter a third integer"); int num3 = sc.nextInt (); int max = (num1> num2 num1: num2?)> num3 (num1>? ? num1 num2: num2): num3; System.out.println ( "maximum value:" + max); . // 2 0 rock scissors cloth 2 is 1, // 0 or a program randomly generated 1 or 2, // user enter a 0 or 1 or 2, mora, win or lose the printing level or a System.out.println ( "0 rock scissors cloth 2 is 1, the program randomly generates a 0 or 1 or 2, a user input 0 or 1 or 2, mora, win or lose the print or a flat"); BREAK; Case. 1:if (num4==2) { Random r= new Random(); r.nextInt Num4 = int (. 3); int Num5; the while (to true) { System.out.println ( "Enter a 0 or 1 or 2"); Num5 sc.nextInt = (); IF (Num5> = 0 && Num5 < . 3) { BREAK; } System.out.println ( "re-enter the correct value of 0 or 1 or 2"); } Switch (Num5) { Case 0: IF (Num4 == 1) { System.out.println ( "lost"); } the else IF (Num4 == 2) { System.out.println ( "win"); } the else { System.out.println ( "the flat"); } the else {} System.out.println ( "the flat"); System.out.println ( "lost"); } the else IF (Num4 == 0) { System.out.println ( "win"); } } BREAK; Case 2: IF (Num4 == 0) { System.out.println ( "lost"); } the else IF (Num4 ==. 1) { System.out.println ( "win"); } the else { System.out.println ( "the flat"); } BREAK; } System.out.println ( "computer random values:" + num4 + "input:" + Num5); .. 3 // user to input 1-7, If the print is 1-5 days, if the printing is 6-7 days rest int Num6; the while (to true) { System.out.println ( "enter 1-7"); Num6 sc.nextInt = (); IF (Num6> =. 1 && Num6 <. 8) { BREAK;. 5 =) { System.out.println ( "days"); System.out.println ( "re-enter the correct value 1-7"); } IF (Num6> =. 1 && Num6 <=. 5) the else {} System.out.println ( "holidays"); } } }