1: The format of the switch statement:
Switch statement: SELECT Structure statement
Format:
switch (expression) {
Case value 1:
Statement 1;
Break
Case Value 2:
Statement 2;
Break
....
Default
Statement n+1;
Break
}
Execution process:
First, the value after the expression after the Siwtch is compared with the value after the case, if the value is equal to 1, EXECUTE statement 1,
If not equal, compare with value 2, set up, execute statement 2, meet break: End
If there is no equal value above, execute the statement in default
Caveats: What is the end condition of the switch statement?
1) Meet break end
2) Program default execution to the end
Cases:
Depending on the value entered, the day of the week is judged. (implemented using the IF statement and the switch statement, respectively)
1) switch statement:
public static void Main (string[] args) {
Creating a Keyboard Entry object
Scanner a = new Scanner (system.in);
Request user Input
System.out.println ("Please enter value (1-7)");
int weeknumber = A.nextint ();
Switch (weeknumber) {
Case 1:
System.out.println ("Monday");
break;
Case 2:
System.out.println ("Tuesday");
break;
Case 3:
System.out.println ("Wednesday");
break;
Case 4:
System.out.println ("Thursday");
break;
Case 5:
System.out.println ("Friday");
break;
Case 6:
System.out.println ("Saturday");
break;
Case 7:
System.out.println ("Sunday");
break;
}
}//(most basic statement output)
2) If statement:
public static void Main (string[] args) {
Creating a Keyboard Entry object
Scanner a = new Scanner (system.in);
Request user Input
System.out.println ("Please enter value (1-7)");
int weeknumber = A.nextint ();
if (WeekNumber = = 1) {
System.out.println ("Monday");
}else if (WeekNumber = = 2) {
System.out.println ("Tuesday");
}else if (WeekNumber = = 3) {
System.out.println ("Wednesday");
}else if (WeekNumber = = 4) {
System.out.println ("Thursday");
}else if (WeekNumber = = 5) {
System.out.println ("Friday");
}else if (WeekNumber = = 6) {
System.out.println ("Saturday");
}else if (WeekNumber = = 7) {
System.out.println ("Sunday");
}
}
Note: the WeekNumber = = 2 in parentheses, must be = =, not =, make judgments rather than assignments.
2The format of the. For loop:
For loop Structure statement
Format:
for (initialize statement; Judge conditional statement; Step statement (Conditional control statement)) {
loop body statement;
}
Execution process:
First, the initialization statement is assigned, and then the conditional statement is established;
If true, then the loop body statement is executed;
After execution, the conditional control statement (++/--) is executed, and the variable is increased by 1 or self minus 1, and then the conditional statement is determined again.
If it is established, continue to execute the Loop body statement;
If the conditional statement is not valid, then the FOR Loop statement ends
Cases:
99 Multiplication Table
public static void Main (string[] args) {
for (int x = 1; x <= 9; x + +) {
for (int y = 1; y <=x; y + +) {
System.out.print (y+ "*" +x+ "=" +y*x+ "\ t");
}
System.out.println ();
}
}
Note: the "\ T" inside can be indented backwards, without causing the number of columns to be continuously unclear. 1.2 is not indented or indented.
3The format of the. While loop:
The format of the while statement:
The basic format:
while (conditional judgment statement) {
loop body statement;
}
Format of the extension:
initialization statements;
while (conditional judgment statement) {
loop body statement;
Control condition statement;
}
Execution process:
The initialization statement is assigned a value and enters the while statement: determines whether the condition is established;
If set, execute the Loop body statement, control the conditional statement to self-decrement, and then come back again
Determine whether the condition is set up, continue to execute the Loop body statement, one analogy.
If not, the while statement ends
Cases:
The number of times the paper folds into Mt. Everest height
The highest mountain in China is Mount Everest: 8848m, I now have a large enough paper, thickness: 0.01m.
Excuse me, how many times do I fold to ensure that the thickness is not lower than the height of Mount Everest?
public static void Main (string[] args) {
Defining statistical variables
int count = 0;
Define initial and final heights
Double start = 0.01;
int end = 8848;
Ambiguous return times with while
while (Start < end) {
Count + +;
Start = start + start;
}
SYSTEM.OUT.PRINTLN ("Need to Fold" +count+ "Times");
}
Java Basic Syntax 1 (looping statements)