Explanation of Java basic code syntax

Posted by luisantonio on Thu, 10 Feb 2022 04:23:39 +0100

Recently, I have spare time to code words. Although the foundation of Java is really basic, it is slow and difficult for me to learn. I think I can't forget that I can't learn linguistics.
I decided to write this article piecemeal, and I want to write it as easy as possible to understand, so as to exercise my ability to speak human words.
Let's come on together.

1: How to scan and output the user's input.

The code is as follows

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Please enter a string:");
        int a = scanner.nextInt();
        int b= scanner.nextInt();
        System.out.println(a);
        System.out.println(b);
    }
}

Syntax format

 Scanner s = new Scanner(System.in);

Create a Scanner object s so that s can call many methods in the Scanner class.
In a word, a class is like a treasure house that stores many methods. If we want to use the methods in this treasure house, we need to give an object to call the methods concretely.

int I = scanner.nextInt.

It means to use the nextInt method under scanner to receive data and give the value of the received data to I

Note 1: the difference between nextInt and nextLine
When nextInt accepts data, if the input is 247, the output is 247. If the input is 2, 4 and 7, the output is 2. It can also be said that spaces cannot be read and output, and the end position is determined by spaces
However, if it is nextLine, input 2 4 7 and output 2 4 7. Enter to judge the end position, and you can output spaces.

2: If you want to use nextInt to receive multiple data at the same time and output it
It's very simple. As I wrote in the code, just give more values of int to receive. I found that by default, the first value a of int in Java receives the first data input, and the second value b of int receives the second data. This is the feature of sequential structure.

2: Three structures of program flow

Sequential selection (condition) and cyclic structure
1: Sequential structure
The printing I mentioned above is a sequential structure, which is executed in sequence
2: Select structure
if single selection structure

Code display

 if(scanner.hasNextInt()) {
            i=scanner.nextInt();//Be sure to receive data!!!
            System.out.println("You entered an integer"+i+"yeah");

        }

If the scanner scans for the next input value, it receives the last input value with i and prints it.

if double selection structure

Code display

 if(scanner.hasNextInt()) {
            i=scanner.nextInt();//Be sure to receive data!!!
            System.out.println("You entered an integer"+i+"yeah");

        }else{
            System.out.println("Hum! The input is not an integer at all");
        }

if multiple selection structure


Code display

if (score<60){
            System.out.println("Fail");
        }else if (score>=60&&score<70){
            System.out.println("The result is D oh");
        }else if (score>=70&&score<80){
            System.out.println("The result is C oh");
        }else if (score>=80&&score<90){
            System.out.println("The result is B oh");
        }else if(score>=90&&score<=100){
            System.out.println("The result is A Oh, that's great");
        }else{
            System.out.println("Big fool, what you entered is an invalid score");
        }

Nested if loop


The code is as follows

  //Nested if loop
    public static void main(String[] args) {
        int i=10,sum=0;
        if (i>=0){
            i=i-1;
            if(sum<=10){
                sum=sum+1;

            }

        }
        System.out.println(i);
        System.out.println(sum);
    }

Note that if is a conditional statement, which will jump out after execution and will not loop, so i=9 and sum=1 are finally output;
switch selection structure


Fill in the expression to be judged in expression, and value is the value to judge whether the expression meets. If it meets, execute the statement, and the code is as follows:

 System.out.println("Please enter your grade");
        Scanner scanner = new Scanner(System.in);

        char grade = scanner.next().charAt(0);


        switch (grade){
            case 'A':
                System.out.println("More than 90% of the students");
                break;
            case 'B':
                System.out.println("More than 80% of the students");
                break;
            case 'C':
                System.out.println("More than 70% of the students");
                break;
            case 'D':
                System.out.println("More than 60% of the students");
                break;
            default:
                System.out.println("You lost wrong again, big fool");

If there is no break, all statements will be executed.

3: Cyclic structure

while Loop

When the Boolean expression is true, the loop will continue to execute
code:

 while (i<=100){
            sum = sum +i;
            i++;
        }
        System.out.println(sum);

First judge whether i is less than or equal to 100. If it is less than 100, the cycle will be executed. If i is not executed once, i will add 1 until i is equal to 101.

do-while Loop

Execute the code statement, and then see whether the Boolean expression is satisfied. If it is satisfied, continue to execute. If not, jump out.
code:

  int i=100;
  do {
            i--;

        }while (i>=2);//Start the cycle as long as I > 2, and terminate the cycle when I < 2
        System.out.println(i);

If i=i-1 is performed first and 99 > 2 is found, the loop will continue to be executed until i=1, and you can jump out of the loop.
for loop

code:

 //2: Use the for loop to output all numbers that can be divided by 5 from 0 to 1000, and output one line every three.
        for (int j = 0; j < 1000; j++) {
            if (j%3==0){
                System.out.print(j+"\t");
            }
            if (j%15==0){
                System.out.print("\n");
            }

        }

Jump out of the loop when j=1000.

Enhanced for loop

public static void main(String[] args) {
        int[] numbers = {1,2,3,4,5,7};
        for(int i=0;i<6;i++){
            System.out.println(numbers[i]);//See how to output an array
        }
        System.out.println("==========");
        for(int x:numbers){//Here is for enhanced loop
            System.out.println(x);
        }
    }

The enhanced loop here will directly complete the traversal and assignment of the array.

Small job print 99 multiplication table

 //Print 99 multiplication table
        for (int t = 1; t <= 9; t++) {
            for (int k = 1; k <= 9; k++) {
                System.out.print(k+"*"+t+"="+t*k+"\t");

            }
            System.out.print("\n");
        }

4: break and continue and Tags

Note: continue jumps out of some statements and break jumps out of the whole statement.
Let's look at continue first, or the code:

 int i=0;
       while (i<100){
           i++;
           if (i%10==0){
               System.out.println();
               continue;//Jump out and re execute while, so SOP cannot be output
           }
           System.out.print(i+" ");
       }

If the value of i is 10, enter, and then meet continue. Continue causes i to jump out of the if statement and continue to execute the contents in while. Therefore, the final code output is as follows:

If break:

 int j=0;
       while(j<100){
           j++;
           if (j==30){
               break;
           }
           System.out.print(j+" ");
       }

When j=30, break not only jumps out of if, but also jumps out of the while loop, so the loop does not continue to execute, but only prints to 29
The output results are as follows:

label
outer: that is, the label we give the program. If there is no label, break and continue will jump to the loop they want to jump, but if there is a label, they will jump to the specified loop.

 outer:for (int k=100;k<=150;k++){
           for(int q =2;q<=k/2;q++){
               if(k%q==0){
                   continue outer;//continue can only jump out of one statement. After labeling, you can jump out of the specified statement
               }
           }
           System.out.print(k+" ");
       }

The prime number is not greater than 150 except for the prime number itself. To judge whether a number is a prime number, divide it by the number less than itself. If there is a remainder, it is a prime number and print. If there is no remainder, it is not a prime number. Skip to for and add one to continue to judge.
Final output:

Small exercise: printing triangles
We want to print such a triangle:
Three sections need to be printed:

Code!

  public static void main(String[] args) {
        for (int i = 0; i <= 5; i++) {

            for (int j = 5; j >i; j--) {//Code 1
                System.out.print(" ");
            }
            for(int j=0;j<i;j++){//Code 2
                System.out.print("*");
            }
            for (int j = 1; j < i; j++) {//Code 3
                System.out.print("*");

            }
            System.out.println();
        }
    }

In order to look clearer, I changed the code again, and the output result is as follows:

i=0, code 1 is executed five times, code 2 is not executed, and code 3 is not executed;
i=1, code 1 is executed four times, code 2 is executed once, and code 3 is not executed;
i=2, code 1 is executed three times, code 2 is executed twice, and code 3 is executed once;
...

I originally wanted to write Debug, but I didn't quite understand some debugging operations and source code checking of Java, so I decided to make a special topic alone.

A very basic part of Java code has been written. Thank crazy God here. A lot of things can be seen through through the code at a glance. Many online courses are also very good. It's just that the content is too comprehensive. It's too hard for me. Coupled with the time limit, even crazy God said that it took me three weeks to learn java completely.
java starts to explain the method. I will write more detailed than this one. Learning is a very happy thing!!!!!

Topics: Java