if (condition) Statement
The condition in the condition statement at this point is that it needs to be enclosed in parentheses.
In fact, the conditional statements in Java are the same as in C + +. Java often wants to execute multiple statements when a condition is true. At this point, we will introduce a concept, that is, "block statement", the specific format is as follows, for reference only:
{
statement1
Statement2
...
}
take the following example, let's try the format above!
if (score>=90)
System.out.println ("excellent"); The result of the desired output at this time is also a state of it!
}
block modules are also referred to as compound statements, where you can place multiple statements in a Java program structure where only one simple statement could have been placed.
The more common cases are the same as in the examples.
title: Input scores, let the program automatically distinguish the grade corresponding to the score!
1 ImportJava.util.Scanner;2 Public classTestsum {3 /**4 * @paramargs5 */6 Public Static voidMain (string[] args) {7 //TODO auto-generated Method Stub8System.out.println ("Please enter fractions");9Scanner sc=NewScanner (system.in);Ten intScore=sc.nextint (); One A if(score>=90){ -System.out.println ("excellent"); -}Else if(score>=70&&score<90){ theSystem.out.println ("Liang"); -}Else if(score>=60&&score<70){ -System.out.println ("Medium"); -}Else{ +System.out.println ("Poor"); - } + } A}
Nesting principles for conditional statements and If-else in Java