Java basic exercise 03 array

Posted by 930913 on Sat, 15 Jan 2022 04:41:47 +0100

1. Define an array to store the scores of 12 students {72,89,65,58,87,91,53,82,71,93,76,68},
Count the number of students at each grade (above 90 is' A ', 8089 is' B', 7079 is' C ', 60 ~ 69 is'D', and below 60 is E),
And put it into the array count, where: count[0] stores the number of people at level E, count[1] stores the number of people at level D,..., and count[4] stores the number of people at level A

```public static void main(String[] args) {
int[] count=new int[5];
int[] a={72,89,65,58,87,91,53,82,71,93,76,68};
for (int i = 0; i < a.length; i++) {
if (a[i]>=90){
count[4]++;
}else if (a[i]>=80&& a[i]<90){
count[3]++;
}else if (a[i]>=70&& a[i]<80){
count[2]++;
}else if (a[i]>=60&& a[i]<70){
count[1]++;
}else {
count[0]++;
}
}
System.out.println("90 Scores above are:"+count[4]);
System.out.println("80~89 Divided into B The are:"+count[3]);
System.out.println("70~79 Divided into C The are:"+count[2]);
System.out.println("60~69 Divided into D The are:"+count[1]);
System.out.println("60 It is divided into E The are:"+count[0]);
}
```

2. Input 8 integers from the keyboard and store them in an array, and then store odd and even numbers into two different arrays respectively,
And output all the data in the two arrays in the alternating order of odd and even numbers
(output alternately first. If there are many odd numbers, then output the remaining odd numbers. If there are many even numbers, then output the remaining even numbers).
(tips and requirements:
(1) Define an array to store 8 integers input from the keyboard. Judge the number of odd and even integers in these 8 integers before defining the length of the array to store odd and even numbers;
(2) The process of storing a large array in odd and even arrays and outputting it alternately is defined as method)

```public static void main(String[] args) {
int[] a=new int[8];

for (int i = 0; i < 8; i++) {
Scanner ar= new Scanner(System.in);
a[i]=ar.nextInt();
}
chose01(a);
}
public static void chose01(int a[]){
int x=0,y=0;
for (int i = 0; i < a.length; i++) {
if (a[i]%2==0){
x++;
} else {
y++;
}
}
int[] o=new int[x];
int[] ji=new int[y];
int r1=-1;
int r2=-1;
for (int i = 0; i < a.length; i++) {
if (a[i]%2==0){
r1++;
o[r1]=a[i];
} else {
r2++;
ji[r2]=a[i];
}
}
int[] b=new int[a.length];

System.out.println("The eight numbers entered are:");
for (int t1:a){
System.out.print(t1+" ");
}
System.out.println();
System.out.println("The odd number is:");
for (int t2:ji){
System.out.print(t2+" ");
}
System.out.println();
System.out.println("The even number is:");
for (int t3:o){
System.out.print(t3+" ");
}
System.out.println();
System.out.println("The parity crossing output is:");
if (ji.length>o.length){
for (int i = 0; i < o.length; i++) {
System.out.print(ji[i]+" "+o[i]+" ");
}
for (int i = o.length; i <ji.length ; i++) {
System.out.print(ji[i]+" ");
}
System.out.println();
}else {
for (int i = 0; i < ji.length; i++) {
System.out.print(ji[i]+" "+o[i]+" ");
}
for (int i = ji.length; i <o.length ; i++) {
System.out.print(o[i]+" ");
}
}
}
```

3. Define an array with 10 array members, find out the maximum number in the array and output it together with the subscript

```public static void main(String[] args) {
int[] a={5,3,1,6,7,2};
int max=a[0];
int x=0;
for (int i = 1; i < a.length; i++) {
if (max<a[i]){
max=a[i];
x=i;
}
}
System.out.println("The maximum value of the array is"+"a["+x+"]="+max);
}
```

4. Given an integer array with 10 members, find the subscript of the second largest number in the array

```public static void main(String[] args) {
int[] a={5,3,1,6,7,2};
int max1=a[0];
int max2=a[0];
int x=0;
int y=0;
for (int i = 1; i < a.length; i++) {
if (max1<a[i]){
max1=a[i];
x=i;
}
}
for (int i = 1; i < a.length; i++) {
if (max2<a[i]&&a[i]!=max1){
max2=a[i];
y=i;
}
}
//        System.out.println("the maximum value of the array is" + "a["+x+"]="+max1);
System.out.println("The second largest value of the array is"+"a["+y+"]="+max2);
}
```

5. Brother B went to the young singer Grand Prix and was scored by 10 judges (excluding the highest and the lowest) to get an average score?

```public static void main(String[] args) {
int[] a={5,3,8,6,7,2,9,6,7,8};
int max=a[0];
int min=a[0];
int sum=0;
double avg;
for (int i = 1; i < a.length; i++) {
if (max<a[i]){
max=a[i];
}else if (min>a[i]){
min=a[i];
}
sum+=a[i];
}
avg=(double) (sum+a[0]-max-min)/(a.length-2);
System.out.println("The maximum value of the array is"+max);
System.out.println("The minimum value of the array is"+min);
System.out.println("The average score after removing the highest score and the lowest score is:"+avg);
}
```

6. Define an array to store the scores of 5 students [set the score value yourself], sort the scores from large to small, and obtain the sum of scores, average score, minimum score and maximum score.

```public static void main(String[] args) {
int[] a=new int[5];
int sum=a[4];
double avg=0;
for (int i = 0; i < 5; i++) {
Scanner sr=new Scanner(System.in);
a[i]=sr.nextInt();
}
//You need to traverse a.length-1 times
for (int i = 0; i < a.length-1; i++) {
//a.length-1-i shall be compared every time
for (int j =i+1; j < a.length; j++) {
if (a[i]<a[j]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
System.out.print("The grades of the five students from small to large are as follows:");
for (int i = 0; i <a.length; i++) {
sum+=a[i];
System.out.print(a[i]+" ");
}
avg=(double)sum/a.length;
System.out.println();
System.out.println("The sum of the five students' scores is:"+sum);
System.out.println("The average scores of the five students are:"+avg);
System.out.println("The top scores of the five students are:"+a[0]);
System.out.println("The minimum scores of the five students are:"+a[4]);
}
```

7. Extract a method to invert the array elements in the specified array
For example: {10,23,2,45,6} - > {6,45,2,23,10}

```public static void main(String[] args) {
int[] a = {10, 23, 2, 45, 6,9};
System.out.println("The original array is:");
for (int x:a){
System.out.print(x+" ");
}
for (int i = 0; i < a.length / 2; i++) {
int temp = a[i];
a[i] = a[a.length - i - 1];
a[a.length - i - 1] = temp;
}
System.out.println();
System.out.println("After inversion, the array is:");
for (int x:a){
System.out.print(x+" ");
}
}
```

8. Now there is an array as follows:
int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
It is required to remove the items with a value of 0 in the above array, and store the values that are not 0 in a new array. The generated new array is:
int[] newArr = {1,3,4,5,6,6,5,4,7,6,7,5} ;

```public static void main(String[] args) {
int[] oldArr = {1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5} ;
int[] newArr = new int[12];
int n=-1;
System.out.println("The original array is:");
for (int x:oldArr){
System.out.print(x+" ");
}
for (int i = 0; i < oldArr.length; i++) {
if (oldArr[i]!=0){
n++;
newArr[n]=oldArr[i];
}
}
System.out.println();
System.out.println("The new array after removing 0 is:");
for (int x:newArr){
System.out.print(x+" ");
}
}
```

9. Now give two arrays:
Array a: "1,7,9,11,13,15,17,19"
Array b: "2,4,6,8,10"
Combine the two arrays into an array c.

```public static void main(String[] args) {
int[] a={1,7,9,11,13,15,17,19};
int[] b={2,4,6,8,10};
int[] c=new int[a.length+b.length];
//Method 1:
System.arraycopy(a,0,c,0,a.length);
System.arraycopy(b,0,c,a.length,b.length);
System.out.println(Arrays.toString(c));
//Method 2:
/*int n=-1;
int m=-1;
for (int i = 0; i <a.length+b.length ; i++) {
n++;
if (i<a.length){
c[n]=a[i];
}else {
m++;
c[n]=b[m];
}
}
System.out.println("a The array is: ");
for (int x:a){
System.out.print(x+" ");
}
System.out.println();
System.out.println("b The array is: ");
for (int y:b){
System.out.print(y+" ");
}
System.out.println();
System.out.println("a And b arrays are merged into: ");
for (int z:c){
System.out.print(z+" ");
}*/
}
```

10. Find a number that appears in the first array array1 and does not appear in the second array array2.

```public static void main(String[] args) {
int[] array1={1,2,3,4,13,5,6,7,8,9};
int[] array2={9,8,1,2,3,4,6,7,9};
System.out.println("In the first array array1 Appears in the second array array2 The numbers that do not appear in are:");
outo: for (int i = 0; i < array1.length; i++) {
for (int j = 0; j < array2.length+1; j++) {
if(j<array2.length){
if (array1[i]!=array2[j]){
continue;
}else {
break;
}
}else {
System.out.print(array1[i]+" ");
//                    break outo;
}
}
}
}
```

Topics: Java