[JavaSE] Logic Control

Table of contents

1. Logic control statements of programs in Java

1.1 Sequence structure

1.2 Branch structure

1.2.1 if statement

1.2.2 switch statement

1.3 Loop structure

2. Input and output

2.1 Output to console

2.2 Input from the keyboard

2.2.2 Use Scanner to read string/integer/float

2.2.3 Implement loop input

3. Guess the number game

1. Logic control statements of programs in Java

1.1 Sequence structure

The sequence structure is relatively simple, and it is executed line by line according to the order in which the code is written.

1.2 Branch structure

1.2.1 if statement

1.2.2 switch statement

Here is a supplementary written test question : What are the data types that cannot be used as switch parameters in Java?

  • long type
  • float type
  • double type
  • boolean type

Switch statement execution flow:
1. Calculate the value of the expression first
2. Compare with case in turn, once there is a response match, execute the statement under this item, until the break is encountered
3. When the value of the expression does not match the listed When the item matches, execute default

switch cannot express complex conditions

// eg: if the value of num is between 10 and 20, print hehe 
// this code is easy to express with if, but not with switch. 
if (num > 10 && num < 20 ) {
    System.out.println("hehe");
}

Although switch supports nesting, it is ugly and generally not recommended~

1.3 Loop structure

  • while loop
  • for loop
  • do while loop

2. Input and output

2.1 Output to console

basic grammar

System.out .println (msg); 
// prints a string with newlines System.out .print (msg); // prints a string without newlines System.out .printf (format, msg) 
; // formatted output

  • The output of println comes with\n, and print does not include\n
  • The formatted output method of printf is basically the same as that of C language printf

code example

System.out.println("hello world");
int x = 10;
System.out.printf("x = %d\n", x)

format string

converter type Example
d decimal integer (“%d”, 100) 100
x hex integer (“%x”, 100) 64
o octal integer (“%o”, 100) 144
f fixed point floating point (“%f”, 100f) 100.000000
e exponent float (“%e”, 100f) 1.000000e+02
g General purpose floating point (“%g”, 100f) 100.000
a hexadecimal floating point number (“%a”, 100) 0x1.9p6
s string (“%s”, 100) 100
c character (“%c”, ‘1’) 1
b Boolean value (“%b”, 100) true
h hash code (“%h”, 100) 64
% percent sign (“%.2f%%”, 2/7f) 0.29%

2.2 Input from the keyboard

2.2.1 The first method (not commonly used)

char i = (char) System.in.read();
System.out.println("your char is:"+i);

When this happens, just press alt+enter.

2.2.2 Use Scanner to read string/integer/float

Scanner scanner = new Scanner(System.in ) ;
 //input integer 
int n = scanner.nextInt();System.out 
.println (n);
 //input float 
float a = scanner.nextFloat();
System.out.println (a);
 // input string
String str= scanner.nextLine();
System.out.println(str);

Some explanation:

When we run the code, some problems are found

while reading the string

2.2.3 Implement loop input

//loop input 
Scanner scanner = new Scanner(System.in ) ;
 while (scanner.hasNextInt()){
 int n=scanner.nextInt();
System.out.println(n);

Note: If you want to see the source code

you can see

3. Guess the number game

import java.util.Random;
import java.util.Scanner;
public class TestDemo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System. in );
         //Generate random numbers 
        //Random random = new Random(); 
        Random random = new Random( 1234 ); //Put a number in parentheses, each time a random number is generated Generated according to this number 
        int randNum = random.nextInt( 100 ); //Enter 100 in parentheses to indicate that the range of random numbers is [0,100) 
        //int randNum2 = random.nextInt(100)+1;//Indicates [1,100 ] or [1, 101) 
        while ( true )
        {
            System.out .println ( "Enter the number you want to guess: " );
             int num= scanner.nextInt();
             if (num< randNum ){System.out 
                .println ( "The guess is too small!" );
            } else  if (num==randNum){System.out 
                .println ( "You guessed it!" );
                 break ;
        }else{System.out 
                .println ( "Guess the big one!" );
            }
        }
    }
}

analyze:

generate random numbers

We call this random number a pseudo-random number. If there is a number in the parentheses, we call this number a random seed. The random number is generated based on this number through a certain algorithm, so when the random seed is determined, The random number generated each time is the same. 

run the program

success!

Leave a Comment

Your email address will not be published. Required fields are marked *