importjava.util.Scanner;publicclassIfElseExample{publicstaticvoidmain(String[]args){Scannerscanner=newScanner(System.in);System.out.println("请输入岳小鹏的期末成绩:");intscore=scanner.nextInt();if(score==100){System.out.println("奖励:宝马 (BMW)");}elseif(score>=80&&score<=99){System.out.p...
In Java, if statement is used for testing the conditions. The condition matches the statement it returns true else it returns false. There are four types of If statement they are: 在Java中,if语句用于测试条件。 条件与返回true的语句匹配,否则返回false。 有四种类型的If语句: For example, if we...
// 导入 Scanner 类用于输入importjava.util.Scanner;publicclassIfElseExample{publicstaticvoidmain(String[]args){// 创建 Scanner 对象用于读取用户输入Scannerscanner=newScanner(System.in);// 提示用户输入一个数字System.out.print("请输入一个数字: ");intnumber=scanner.nextInt();// 判断数字是否大于零if...
Note thattheelseblock is optional. We may write a statement as : if(condition){//statement-1} 2. The If-else Example Let’s see an example of anif-elsestatement. In the following program, we check whether the employee’s age is greater than 18. On both bases, we print that the em...
This example shows how you can use if..else to "open a door" if the user enters the correct code:ExampleGet your own Java Server int doorCode = 1337; if (doorCode == 1337) { System.out.println("Correct code. The door is now open."); } else { System.out.println("Wrong code....
Example 3: if-else if-else Statementpublic class IfElseIfElseExample { public static void main(String[] args) { int number = 7; if (number > 10) { System.out.println("The number is greater than 10."); } else if (number > 5) { System.out.println("The number is greater than 5...
1) Simple Java if-else example The following example shows how to use theif-elsestatement to display different messages based on the value of a variabletemperature: publicclassApp{publicstaticvoidmain(String[] args){inttemperature =25;if(temperature >30) { System.out.println("It's hot outsid...
intprice;if(condition1){ price =1; }elseif(condition2) { price =2; }else{ price =0; } 优化后 intprice=condition1 ?1: (condition2 ?2:0); 3、使用Optional 我们在代码中判null会导致存在大量的if-else,这个时候我们可以考虑使用Java8的Optional去优化。
Example 1: Basicif...elseStatement publicclassIfElseExample{publicstaticvoidmain(String[]args){intnumber=10;if(number>0){System.out.println("The number is positive.");}else{System.out.println("The number is not positive.");}}} In this example, the program checks if thenumberis greater ...
if( if any thing here true ){ do the thing here. } else if ( else if other thing true ){ Do the function here } else { Do if every booleans above false } 9th May 2019, 9:17 AM Basel.Al_hajeri?.MBH() + 4 IF ELSE IF (×any number of times) ELSE Basically you'...