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...
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....
// 导入 Scanner 类用于输入importjava.util.Scanner;publicclassIfElseExample{publicstaticvoidmain(String[]args){// 创建 Scanner 对象用于读取用户输入Scannerscanner=newScanner(System.in);// 提示用户输入一个数字System.out.print("请输入一个数字: ");intnumber=scanner.nextInt();// 判断数字是否大于零if...
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 employee is a minor or an adult. intage=employee.getAge();if(age>18){System.out.println("Employe...
In this example, the condition number > 5 evaluates to true because number is 10. Therefore, the message "The number is greater than 5." is printed to the console.Example 2: if-else Statementpublic class IfElseExample { public static void main(String[] args) { int number = 3; if (...
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去优化。
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'...
}else{ //block of code to be executed if the condition1 is false and condition2 is false } Example If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00, create a "Good day" greeting, otherwise a "Good evening": ...
}else { price = 0; } 优化后 int price = condition1 ? 1 : (condition2 ? 2 : 0); 3、使用Optional 我们在代码中判null会导致存在大量的if-else,这个时候我们可以考虑使用Java8的Optional去优化。 优化前 public static void main(String[] args) { ...