Judgment statement
1,switch--case
Processing branch structure, can be implemented using if-else, in turn, does not hold
equally suited to handle a limited number of specific comparison value
break: Termination condition, there is no break to continue following statements
note:
type or variable expression only after the switch; can be automatically converted to an int, a string, enumeration
value not repeat the case
value is not the case the variable, only a constant
Classroom tasks:
A letter (a, b, c, d, e) using the input from the keyboard switch - case structure for uppercase letters
= New new Scanner Scanner Scanner (the System.in);
String Scanner.next S = (); // get the keyboard input string
// char = S Scanner.next () the charAt (0);.
Switch (S) {
Case "A":
System.out.println ( "A");
BREAK;
default:
System.out.println ( "invalid input");
}
2, if the statement
1,
IF ( B ) {// make a judgment variable is boolean type } the else { }
2,
IF (expression The) {// expression The expression is a } the else { } . 3, IF (expression1 The) { } the else IF (expression2 The); { } the else IF (of expression3) { } the else IF (expressionN) { } // the else { Alternatively, recommendation is not omitted }
2, cycle
Repeat the cycle to solve statement, generally there are times
for();
for(statment1;stetement2;statement3){
statement4....
}
The first step: first implementation statment1, the loop variable initialization
Step two: performing stetement2, obtained as a result, the result is a boolean
If the result is true, statement execution statement4 loop body
if the result is false, the loop terminates
Third Step: When the execution loop, of statement3 in the execution, change the loop variable
Repeat step
Special case:
for (;;) {
System.out.println (. 1);
} // infinite loop
while;
while(a){b};
// a determination is true, continue to determine the genuineness of a B after performing true, complete, when a is false, the out of the loop.
do{a}while();
// performed first and then a judgment
Case: integer between output 1-100
for (int i = 1; i <= 100; i++) { System.out.println(i); }
Classroom tasks:
1, the even between 1-100
2, between the output 100-999 or with an integer multiple of 7 7
public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 100; i < 1000; i++) { int a = i / 100;// baiweishu int b = (i % 100) / 10;// shiweishu int c = i % 10;// geweishu if (i % 7 == 0 || a == 7 || b == 7 || c == 7) { System.out.println(i); } } }
3, and between the calculated odd 1-100
public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i < 100; i++) { if (i % 2 == 0) { System.out.println(i); } } }
4, is calculated by multiplying 5 fold between 1-50
public static void main(String[] args) { // TODO Auto-generated method stub long total=1; for(int i=1;i<=50;i++){ if(i%5==0){ total=total*i; } } System.out.print(total); }
5, a positive integer from keyboard input, it is determined whether the number is an integer (prime) mass
public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); int s = scanner.nextInt(); int q = 1; for (int i = 2; i < s; i++) { if (s % i == 0) { q = 0; break; } } if (q == 1) { System.out.print("right"); } else { System.out.print("wrong"); } }
1, while the even achieved between 1-100
2, given an integer, the size of an integer between the input from the keyboard, compare it with the previous given integer, and prompt until fishes, guess how many times the output
Tip: while (true) to terminate the use of break cycle
static void main public (String [] args) { // the TODO Auto-Generated Method Stub Scanner Scanner Scanner new new = (the System.in); int scanner.nextInt Y = (); // original price int. 1 = S; int I . 1 =; the while (I <100) { int scanner.nextInt C = (); // guess price IF (C> Y) { of System.out.print ( "high"); } IF (Y> C) { System.out.print ( "low"); } IF (Y == C) { System.out.print ( "Yes"); BREAK; } S ++; } System.out.print ( "you only measure the "+ s +" views "); }
1, calculated on the number of the respective bits and any positive integer
12 is. 3 1234 10
2, seeking 2 / 1,3 / 2,5 / 3,8 / 5,13 / 8 ... and the number of columns of the front 10 (Note that the data type)
3, 1000 in the output of a prime number
4, the shape of the output (the relationship between the number of times the inner loop and the outer loop)
*
* *
* * *
* * * *
------- -------------
* * * *
* * *
* *
*
5, is known to Monday 1 January 1900, enter the month and year from the keyboard, and outputs the calendar
, such as: 2019 7 outputs
day one hundred twenty-three thousand four hundred fifty-six
. 1 2. 6. 5. 4. 3
7 10. 11. 8. 9 13 is 12 is
14 15. 19 16. 17 18 is 20 is
21 is 22 is 23 is 26 is 24 25 27
28 29 30 31
The whole text much, practice a lot, very real test of logic and handwriting (for beginners)