java-Chapter 3 Flow Control Statement

Experiment content:

     1. Determine the parity of integers.

     2. Output the number of all daffodils.

    3. Guess the number game.

    4. Enter and count student results

Experimental steps:

1. Input an integer from the keyboard, judge its parity and output it.

Tip: Example of how to enter data from the console:

System.out.println("请输入一个整数:");

        Scanner scan=new Scanner(System.in);

        int num=scan.nextInt();

 

Source code:

package sy3;

import java.util.Scanner;

public class Sy_1 {

    public static void main(String[] args){

        System.out.println("请输入一个整数:");

        Scanner sc = new Scanner(System.in);

        int num = sc.nextInt();

        if(num%2 == 0)

           System.out.printf(num+"是偶数");

        else{

           System.out.printf(num+"是奇数");

        }

    }

}

Screenshot of running result:

2. Output the number of all daffodils. The so-called daffodil number refers to a three-digit integer whose cube sum of each digit is equal to itself, for example: 153=13+53+33. It is required to output the result in one line.

Source code:

package sy3;

public class Sy3_2 {

    public static void main(String[] args){

        int x=0,y=0,z=0;

        for(int i=100;i<1000;i++) {

           x = i/100;

           y = (i%100)/10;

           z = (i%100)%10;

           if(i == x*x*x+y*y*y+z*z*z){

               System.out.println(i+"是水仙花");

           }

           }

        }

}

Screenshot of running result:

3. Guess the number game: write a Java application to achieve the following functions:

(1) The program randomly assigns an integer between 1 and 100 to the customer.

(2) The user enters his guess from the console.

(3) The program returns a prompt message on the console. The prompt messages are "Guess the big", "Guess the small" and "Guess the right".

(4) The user can input the guess again according to the prompt message until the prompt message is "Congratulations, you guessed it!".

Source code:

package sy3;

import java.util.Scanner;

public class Sy3_3 {

    public static void main(String[] args){

        System.out.println("请输入一个整数:");

        Scanner sc = new Scanner(System.in);

        int num = (int)(Math.random()*100)+1;

        int guess = sc.nextInt();

        System.out.println("请输入你的猜测:");

       

        while(guess != num){

        if(guess > num){

           System.out.println("猜大了"); 

           guess = sc.nextInt();

        }

        else if(guess < num){

           System.out.println("猜小了");

           guess = sc.nextInt();

        }

        }

           System.out.println("恭喜你,猜对了");

        }

    }

   

 

Screenshot of running result:

 

4. Enter a batch of student grades, with -1 as the end mark.

(1) Count the number of students who failed, passed, medium, good, and excellent in this batch of students.

(2) Find the average score of this group of students.

Tip: Example of how to enter grades from the console:

Scanner scan=new Scanner(System.in);

System. out .println( " Please enter a batch of student results, with -1 as the end tag :" );

Source code:

package sy3;



import java.util.Scanner;



public class Sy3_4 {

    public static void main(String[] args){

        int score=0,a=0,b=0,c=0,d=0,e=0,sum=0;

        Scanner sc = new Scanner(System.in);

        System.out.println("请输入成绩,输入-1查看结果:");

        score = sc.nextInt();

       

       

        while(score != -1){

           if(score>=90){

               System.out.println("优秀");

               a++;

           }

           else if(score>=80){

               System.out.println("良好");

               b++;

           }

           else if(score >= 70){

               System.out.println("中等");

               c++;

           }

           else if(score >= 60){

               System.out.println("及格");

               d++;

           }

           else{

               System.out.println("不及格");

               e++;

           }

           sum += score;

           score = sc.nextInt();

        }

       

       

       

        System.out.println("成绩优秀的人有:"+a);

        System.out.println("成绩良好的人有:"+b);

        System.out.println("成绩中等的人有:"+c);

        System.out.println("成绩及格的人有:"+d);

        System.out.println("成绩不及格的人有:"+e);

       

       

        double average = sum*1.0/(a+b+c+d+e);

        System.out.printf("平均分:" + average);

       

    }



}

Screenshot of running result:

Experiment summary

Through the study of this chapter, I have learned about several commonly used loop statements and conditional statements.

Conditional sentence: if conditional sentence has three forms

1: if (expression) {method body}

2: if (expression) {method body} else {method body}

3: if (expression) {method body} else if (expression) {method body} else{method body}

 

Understand the loop statement:

Including while loop statements, do...while loop statements and for loops. While (Boolean expression) is similar to do...while (Boolean expression). While is judged first and then executed, do...while is executed once and then the condition is judged. If the result of the Boolean expression is true, then the results of the two loop statements are the same. If the first result of the Boolean expression is false, do...while will be executed once, while while will not continue.

For loop format: for (variable initialization; conditional judgment; stepping) {loop statement}, only one step is initialized, and then the conditional judgment is performed. After it is true, the loop statement in the for is executed, and the step is performed after execution, and then the condition is continued Judge until the result is false, jump out of the loop

 

Guess you like

Origin blog.csdn.net/qq_45176548/article/details/112262310