Java randomly generates 12 integers, stores them, and outputs their maximum and minimum values

Table of contents

I. Introduction

Two, the code part

3. Program running results (the panel pops up) 

4. Codes of knowledge points involved


I. Introduction

1. 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!

2. This pop-up window interface can be input according to simple requirements, and display whether it is correct or not. The code setting of this article is to randomly generate 12 integers in the code, save them, and output the maximum and minimum values. At the same time, custom settings can be realized;

3. Randomly generate 12 integers, store them in, and output their maximum and minimum values. This is implemented with a two-dimensional array. After the value is randomly generated, the generated value is cycled to generate the maximum and minimum values, which are displayed and the position of the value can be realized at the same time;

4. The system can only run on the console (eclipse and other versions), and needs to be matched with the jdk environment;

5. Here is a special note, if the complete code package name to be pasted is inconsistent with mine, it is specified inconsistently, please change it manually;

Two, the code part

 1. One-dimensional array to achieve bubble sort code

//随机产生12个整数,存入
package com.edu.s5;

public class test3 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
          int a[][]=new int[3][4];//声明并创建二维数组a
          int max,min,maxrow=0,maxcol=0,minrow=0,mincol=0;
          System.out.println("数组a为:");
          for(int i=0;i<a.length;i++){//i=0~2,控制行数
        	  for(int j=0;j<a[0].length;j++){//j=0~3,控制每行中的列数
        		  a[i][j]=(int)(Math.random()*100+1);
        		  System.out.print(a[i][j]+"\t");
        	  }
        	  System.out.println();
          }
          max=a[0][0];
          min=a[0][0];
          for(int i=0;i<a.length;i++){
        	  for(int j=0;j<a[0].length;j++){
        		  if(a[i][j]>max){
        			  max=a[i][j];
        			  maxrow=i;
        			  maxcol=j;
        		  }
        		  if(a[i][j]<min){
        			  min=a[i][j];
        			  minrow=i;
        			  mincol=j;
        		  }
        	  }
          }
          System.out.println("max["+maxrow+"]["+maxcol+"]="+max);
          System.out.println("min["+minrow+"]["+mincol+"]="+min);
          System.out.println("max="+max+",min="+min);
	}

}

3. Program running results (the panel pops up) 

1. Display the results (first run)

 2. Display the results (run a second time)

 

4. Codes of knowledge points involved

1. The code uses multiple loop statements;

for(int i=0;i<a.length;i++)
        {             System.out.print(a[i]+" ");//The value of each element in the output array         }

Guess you like

Origin blog.csdn.net/weixin_59042297/article/details/129783556
Recommended