当前位置:网站首页>Day 6 (array example)
Day 6 (array example)
2022-07-24 19:18:00 【to be__】
1、 Print Yang Hui triangle with two-dimensional array
class project {
public static void main(String[] arg) {
// Print a 10 Go Yanghui triangle
int[][] arr = new int[9][];
for(int i =0; i<arr.length; i++){
arr[i] = new int[i + 1]; // The first i Yes i+1 Elements
arr[i][0] = arr[i][i] =1; // The first and last elements are 1
if (i>1){ // Start with the third line
for(int j = 1;j<arr[i].length-1;j++){
arr[i][j] = arr[i-1][j-1] + arr[i-1][j];
}
}
}
for (int i = 0; i<arr.length;i++){
for (int j = 0;j<arr[i].length;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
}
}2、 Create a length of 6 Of int Type of the array , The required value is 1-30, At the same time, the element values are different
class project {
public static void main(String[] arg) {
int[] arr = new int[6];
for (int i=0;i<arr.length;i++){
arr[i] = (int)(Math.random()*30)+1; //[0,1) [0,30) [1,31)
for (int j=0;j<i;j++){
if (arr[i]==arr[j]){
i--; // return , Reassign
break; // Go back to the first for Loop continuation i++, That is to regenerate random numbers
}
}
}
for(int j = 0;j<arr.length;j++){
System.out.println(arr[j]);
}
}
}3、 Define a int One dimensional array of type , contain 10 Elements , Assign some random integers , Then find the maximum value of all elements , minimum value , Average , The sum of the , And output . requirement : All random numbers are double digits
import java.util.Scanner;
class project {
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
int account = 0;
float mean = 0l;
int[] arr = new int[10];
for (int i = 0; i<arr.length;i++){
arr[i] = (int)(Math.random()*(90-10+1)+10); //[10,99]
System.out.println(arr[i]);
}
int max = arr[0];
for (int j = 0; j<arr.length;j++){
if (max < arr[j]){
max = arr[j];
}
}
System.out.println("the max number of the arr is:"+max+"\n");
int min = arr[0];
for (int j = 0; j<arr.length;j++){
if (min > arr[j]){
min = arr[j];
}
}
System.out.println("the min number of the arr is:"+min+"\n");
for (int j = 0; j<arr.length;j++){
account += arr[j];
}
System.out.println("the sum of the arr is:"+account+"\n");
mean = account/arr.length;
System.out.println("the avg of the arr is:"+mean+"\n");
}
}4、 Linear search arr Element value specified in
class project {
public static void main(String[] arg) {
String[] arr = new String[]{"aa","bb","cc","dd","ee"};
for (int i = 0; i<arr.length;i++){
System.out.println(arr[i]);
}
String dest = "ee";
boolean flag = true;
for (int j = 0; j<arr.length; j++){
if (dest.equals(arr[j])){ // stay arr[] Medium linear search and dest Equal element values , And return the index
System.out.println(" The specified value was found , Location is :"+j);
flag = false;
break;
}
}
if (flag){
System.out.println(" Did not find ");
}
}
}边栏推荐
- Meshlab & PCL ISS key points
- Siyuan notes V2.1.2 synchronization problem
- Typora user manual
- Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack
- Tcl/tk file operation
- MySQL final chapter
- MySQL sort. Sort by field value
- Emergency lighting design of large stadiums and gymnasiums
- FPGA 20 routines: 9. DDR3 memory particle initialization write and read through RS232 (Part 2)
- MySQL index principle and query optimization "suggestions collection"
猜你喜欢

Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack

MySQL1

How does PostgreSQL decide PG's backup strategy

Tcl/tk file operation

Convolutional neural network CNN

Machine learning_ Data processing and model evaluation

MySQL1

Convolution neural network receptive field calculation Guide

PWN learning

OpenGL learning (IV) glut 3D image rendering
随机推荐
In the spring of domestic databases
Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack
Cesium uses czml to implement dynamic routes
Detailed explanation of DHCP distribution address of routing / layer 3 switch [Huawei ENSP]
Arrays
MySQL8.0学习记录19 - 页区段与表空间
Serial vector format (SVF) file format
Those gods on Zhihu reply
On July 31, 2022, the dama-cdga/cdgp data governance certification class was opened!
Sqoop
Meshlab&PCL ISS关键点
Installation and use of lsky Pro lancong drawing bed: a drawing bed program for online uploading and managing pictures
Modelarts, Pangu big model, ModelBox... Detailed explanation of Huawei cloud AI development production line
asp. Net coree file upload and download example
OpenGL learning (IV) glut 3D image rendering
Ensure the health and safety of front-line construction personnel, and implement wrong time construction at Shenzhen construction site
Colon sorting code implementation
Zooinspector Download
Pay close attention! List of the latest agenda of 2022 open atom open source Summit
Nacos简介和控制台服务安装