Java realizes the two program codes of inputting three numbers and outputting these three numbers from small to large, and inputting students' grades into an array to output grades

Table of contents

foreword

1. Input three numbers and output the three numbers from small to large

1.1 Operation process (thought)

1.2 code snippet

1.3 run screenshot

2. Enter the student's grades and store them in the array to output grades

1.1 Operation process (thought)

1.2 code snippet

1.3 run screenshot


foreword

1. Due to multiple reasons, this blog post consists of two code programs. If you have a choice, you can quickly search in the directory;

2. This pop-up window interface can realize one-use functions according to simple requirements. At the same time, custom settings can be realized;

3. The system can only run on the console (eclipse and other versions), and needs to be matched with jdk8 and other environments;

4. Here is a special note, if the package name of the complete code to be pasted is inconsistent with mine, the program is specified to be unable to run, please change it manually;

5. This code was written by me when I was in school. There are some places that have not been perfectly implemented. Please forgive me and give me more advice. If you find any problems, please give me feedback, so that I can correct mistakes, summarize and improve, Continuous improvement! 


提示:以下是本篇文章正文内容,下面案例可供参考

1. Input three numbers and output the three numbers from small to large

1.1 Operation process (thought)

This is a definition of parameter content in Java, and then you need to define three integer variables and input their values ​​​​in turn, and then use the for loop to judge and assign values ​​(here you need to assign variables to other values ​​​​to store intermediate variables), and finally carry out program output. The specific setting idea is as follows:

1.2 code snippet

The code is as follows (example):

package 程序基础;

import java.util.Scanner;

public class 输入三个整数xyz请把这三个数由小到大输出 {
	
		public static void main(String[] args) {
			int t=0;
			Scanner in=new Scanner(System.in);	
			System.out.println("请输入三个整数:");
			int x=in.nextInt();
			int y=in.nextInt();
			int z=in.nextInt();
			if(x>y){
				t=y;
				y=x;
				x=t;
			}
			if(x>z){
				//a,c交换
				t=z;
				z=x;
				x=t;
			}
			if(y>z){
				t=z;
				z=y;
				y=t;
			}
			System.out.println(x+","+y+","+z);
		}

	}

1.3 run screenshot

The code is as follows (example):

2. Enter the student's grades and store them in the array to output grades

1.1 Operation process (thought)

This is to define the parameter content in Java, and then need to define an array to store variables, use the if else if statement for loop judgment, and finally perform program output. The specific setting idea is as follows:

1.2 code snippet

The code is as follows (example):

package 程序基础;

import java.util.Scanner;

public class 编写程序键盘输入5名学生成绩存入数组中输出相应等级并计算总分等 {

	public static void main(String[] args) {
		// TODO 自动生成的方法存根
	

		int sum = 0;
		float avg=0;
		Scanner in= new Scanner(System.in);
        int score[]=new int[5];
        for (int i = 0; i <score.length; i++) {
        	System.out.println("您输入第"+(i+1)+"名同学的成绩");
        	score[i]=in.nextInt();
            if (score[i]< 0 || score[i]> 100) {
                System.out.println("您输入的数据是一个非法数据");
            } else if (score[i]<60) {
                System.out.println("不及格");
            } else if (score[i] < 70) {
                System.out.println("及格");
            } else if (score[i]< 80) {
                System.out.println("中等");
            } else if (score[i]< 90) {
                System.out.println("良好");
            } else {
                System.out.println("优秀");
            }
            sum += score[i];
        }
        avg=sum/5.0f;
        System.out.println("这五名学生总分为:" + sum);
        System.out.println("这五位同学的平均成绩为:" + avg);
    }
}

1.3 run screenshot

The code is as follows (example):

 

Guess you like

Origin blog.csdn.net/weixin_59042297/article/details/129986765