当前位置:网站首页>Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure

2022-07-01 07:54:00 Onycho

Scanner object

 Insert picture description here

next() and nextLine()

next()

 Insert picture description here

package com.jiao.scanner;


import java.util.Scanner;

public class Demo01 {
    

    public static void main(String[] args) {
    

        // Create a scan object , For receiving keyboard data 
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Use next Send and receive :");

        // Determine whether the user has input string 
        if (scanner.hasNext()){
    
            // Use next Send and receive 
            String str = scanner.next(); // The program will wait for the user to input 
            System.out.println(" The input content is :"+str);
        }
        // All belong to IO( Input 、 Output ) If the class of stream is not closed, it will occupy resources all the time 
        scanner.close();
    }
}
 Output 
 Use next Send and receive :
hello world
 The input content is :hello

nextLine()

 Insert picture description here

package com.jiao.scanner;


import java.util.Scanner;

public class Demo02 {
    
    public static void main(String[] args) {
    
        // Receive data from keyboard 
        Scanner scanner = new Scanner(System.in);
        
        System.out.println(" Use next Send and receive :");

        // Judge whether there is any input 
        if(scanner.hasNext()){
    
            String str = scanner.nextLine();
            System.out.println(" The input content is :"+str);
        }
        scanner.close();
    }
}
 Output 
 Use next Send and receive :
hello world
 The input content is :hello world
package com.jiao.scanner;


import java.util.Scanner;

public class Demo03 {
    
    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Please input data :");
        String s = scanner.nextLine();
        System.out.println(" The output content is :"+s);
        scanner.close();

    }
}
 Output 
 Please input data :
 Welcome to    Study   java
 The output content is : Welcome to    Study   java

practice : We can enter multiple numbers , And find the sum and average , Press enter to confirm each number entered , End the input and output the execution result by inputting non number

package com.jiao.scanner;


import java.util.Scanner;

public class Demo05 {
    
    public static void main(String[] args) {
    
        // We can enter multiple numbers , And find the sum and average , Press enter to confirm each number entered , End the input and output the execution result by inputting non number 
        Scanner scanner = new Scanner(System.in);

        // and 
        double sum = 0;
        // Calculate how many numbers are entered 
        int m = 0;
        System.out.println(" Please start typing :");
        // Loop to see if there is any input , And sum up every time in it 
        while (scanner.hasNextDouble()){
    
            double v = scanner.nextDouble();

            m = m + 1; //m++
            sum = sum + v;
            System.out.println(" You entered the number one "+m+" Data , Then the current result is sum="+sum);
        }
        System.out.println(m + " The sum of the numbers is " + sum);
        System.out.println(m + " The average number is " + (sum / m));

        scanner.close();

    }
}
 Output 
 Please start typing :
2
 You entered the number one 1 Data , Then the current result is sum=2.0
88
 You entered the number one 2 Data , Then the current result is sum=90.0
911
 You entered the number one 3 Data , Then the current result is sum=1001.0
l
3 The sum of the numbers is 1001.0
3 The average number is 333.6666666666667

Sequential structure

 Insert picture description here

package com.jiao.struct;

public class ShunXuDemo {
    
    public static void main(String[] args) {
    
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
    }
}
 Output 
hello1
hello2
hello3
hello4

Selection structure

 Insert picture description here

 Insert picture description here

package com.jiao.struct;


import java.util.Scanner;

public class IfDemo01 {
    
    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);

        System.out.println(" Please enter a content :");
        String s = scanner.nextLine();


        //equals: Determines whether the strings are equal 
        if (s.equals("Hello")){
    
            System.out.println(s);
        }
        System.out.println("End");
        scanner.close();
    }
}

 Output 
 Please enter a content :
Hello
Hello
End

 Insert picture description here

package com.jiao.struct;


import java.util.Scanner;

public class IfDemo02 {
    
    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Please enter the grade :");
        int score = scanner.nextInt();
        if (score > 60){
    
            System.out.println(" pass ");
        }else{
    
            System.out.println(" fail, ");
        }


        scanner.close();
    }
}
 Output 
 Please enter the grade :
50
 fail, 

 Insert picture description here

package com.jiao.struct;


import java.util.Scanner;

public class IfDemo03 {
    
    public static void main(String[] args) {
    
        Scanner scanner = new Scanner(System.in);
        System.out.println(" Please enter the grade :");
        int score = scanner.nextInt();
        if (score == 100){
    
            System.out.println(" Full marks ");
        }else if(score < 100 && score >= 90){
    
            System.out.println("A");
        }else if(score < 90 && score >= 80){
    
            System.out.println("B");
        }else if(score < 80 && score >= 60){
    
            System.out.println("C");
        }else if(score < 60 && score >= 0){
    
            System.out.println(" fail, ");
        }else{
    
            System.out.println(" The score entered is not legal ");
        }

        scanner.close();

    }
}
 Output 
 Please enter the grade :
80
B

 Insert picture description here
 Insert picture description here

package com.jiao.struct;
    //case through 
    //switch  Match a specific value 
public class SwitchDemo01 {
    
    public static void main(String[] args) {
    

        char grade = 'B';
        switch (grade){
    
            case'A':
                System.out.println(" good ");
                break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output 
            case'B':
                System.out.println(" good ");
                break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output 
            case'C':
                System.out.println(" pass ");
                break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output 
            case'D':
                System.out.println(" Make persistent efforts ");
                break; // Optional , If not, it will happen case Penetration phenomenon , The following statement results will be output 
            default:
                System.out.println(" Unknown level ");

        }
    }
}
 Output 
 good 

jdk7 New characteristics

package com.jiao.struct;

public class SwitchDemo02 {
    
    public static void main(String[] args) {
    
        String name = " Li Ming ";

        switch (name){
    
            case " Zhang San ":
                System.out.println(" Zhang San ");
                break;
            case " Li Ming ":
                System.out.println(" Li Ming ");
                break;
                default:
                System.out.println(" Illegal input ");
        }

    }
}
 Output 
 Li Ming 

Loop structure

 Insert picture description here
 Insert picture description here

package com.jiao.struct;

public class WhileDemo01 {
    
    public static void main(String[] args) {
    

        // Output 1~100

        int i = 0;
        while (i < 100){
    
            i++;
            System.out.println(i);
        }
    }
}
 Output 
1
2
、、、
100
package com.jiao.struct;

public class WhileDemo02 {
    
    public static void main(String[] args) {
    
        // Dead cycle 
        while (true){
    
            // Waiting for the customer to connect 
            // Timing check 
        }
    }
}

Calculation 1+2+3+…+100 = ?

package com.jiao.struct;

public class WhileDemo03 {
    
    public static void main(String[] args) {
    
        // Calculation 1+2+3+...+100 = ?

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

 Insert picture description here

package com.jiao.struct;

public class DoWhileDemo01 {
    
    public static void main(String[] args) {
    
        int i = 0;
        int sum = 0;

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

}
 Output 
5050
package com.jiao.struct;

public class DoWhileDemo02 {
    
    public static void main(String[] args) {
    
        int a = 0;
        while (a < 0){
    
            System.out.println(a);
            a++;
        }
        System.out.println("================");
        do {
    
            System.out.println(a);
            a++;
        }while (a < 0);
    }
}
 Output 
0

 Insert picture description here

package com.jiao.struct;

public class ForDemo01 {
    
    public static void main(String[] args) {
    
        int a = 1; // Initialization conditions 
        while (a <= 100) {
     // conditional 
            System.out.println(a); // The loop body 
            a += 2; // iteration 
            }
        System.out.println("while The loop ends ");

        // initialize value 、 conditional 、 iteration 
        for (int i = 1;i <= 100;i++){
    
            System.out.println(i);
        }
        //100.for  Fast generation  for (int i = 0; i < 100; i++)
        
        System.out.println("for The loop ends ");
    }
}


practice

practice 1: Calculation 0 To 100 The sum of odd and even numbers between

package com.jiao.struct;

public class ForDemo02 {
    
    public static void main(String[] args) {
    
        // practice 1: Calculation 0 To 100 The sum of odd and even numbers between 

        int oddSum = 0;
        int evenSum = 0;

        for (int i = 0; i <= 100; i++) {
    
            if (i % 2 != 0){
    
                oddSum += i;
            }else {
    
                evenSum += i;
            }
        }
        System.out.println(" Odd sum "+oddSum);
        System.out.println(" An even sum "+evenSum);
    }
}
 Output 
 Odd sum 2500
 An even sum 2550

practice 2: use while or for Cyclic output 1~1000 Inter energy 5 Divisible number , And output each line 3 individual

package com.jiao.struct;

public class ForDemo03 {
    
    public static void main(String[] args) {
    
        // practice 2: use while or for Cyclic output 1~1000 Inter energy 5 Divisible number , And output each line 3 individual 
        for (int i = 0; i <= 1000; i++) {
    
            if(i % 5 == 0){
    
                System.out.print(i+"\t");
            }
            if(i % (5*3) == 0){
    
                //System.out.println();  It can also be realized 
                System.out.println("\n");
            }

        }
        //println  Line wrap after output 
        //print  The output will not wrap 
    }
}

 Output 
0	

5	10	15	

20	25	30	

35	40	45	

50	55	60	

、、、

practice 3: Output 9X9 Multiplication table

package com.jiao.struct;

public class ForDemo04 {
    
    // practice 3: Output 9X9 Multiplication table 
    public static void main(String[] args) {
    
        //1. Print the first column first 
        //2. Fix it 1 Wrap it up again by one person 
        //3. Remove duplicates  i <= j
        //4. Adjust the style 
        
        for (int j = 1; j <= 9; j++) {
    
            for (int i = 1; i <= j; i++) {
    
            System.out.print(j + "*" + i + "=" + (j * i) + "\t");
            }
            System.out.println();
        }

    }
}

 Insert picture description here

package com.jiao.struct;

public class ForDemo05 {
    
    public static void main(String[] args) {
    
        int[] numbers = {
    10,20,30,40,50}; // An array is defined 

        for (int i = 0;i < 5;i++) {
    
            System.out.println(numbers[i]);
        }
        System.out.println("=============");
        // Traversing array elements 
        for (int x:numbers){
     // hold number Assign a value to int
            System.out.println(x);
        }
    }
}
 Output 
10
20
30
40
50
=============
10
20
30
40
50



原网站

版权声明
本文为[Onycho]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202160156158164.html