Java Array Exercise 1

Keywords: Java Programming

Programming problems

Topic 1

Simulated Grand Lottery Number:

A group of big lottery numbers is composed of 10 numbers between 1 and 99 to print the information code of big lottery numbers. The effect is shown in the figure.

Code implementation:

import java.util.Random;

public class Test01 {
	public static void main(String[] args) {
		
		System.out.println("your big lottery number is:");
		// Declare and create arrays
		int[] array = new int[10];
		Random random = new Random();
		for(int i = 0; i < array.length; i++){
			array[i] = random.nextInt(99)+1;
		}
		
		for(int i = 0;i < array.length; i++){
			System.out.print(array[i]+" ");
		}
		
		
	}

}

Question 2

Print playing cards

Code implementation, the effect is shown as follows:

Code implementation:

public class Test02 {
	public static void main(String[] args) {
		//Flower color
		String[] strFlower = {"Spade","Heart","Plum blossom","Square sheet"};
		//Point
		String[] strNumber = {"A","1","2","3","4","5","6","7","8"
					,"9","10","J","Q","K"};
		for(int i=0; i<strFlower.length;i++){
			for(int j=0; j<strNumber.length; j++){
				System.out.print(strFlower[i]+strNumber[j]+"  ");
			}
			System.out.println();
		}
	}
}

Question 3

Simulated in a deck of cards, draw the first card, the fifth card, the fiftieth card.

Code implementation, the effect is shown as follows:

Code implementation:

public class Test03 {
	public static void main(String[] args) {
		//Flower color
		String[] strFlower = {"Spade","Heart","Plum blossom","Square sheet"};
		//Point
		String[] strNumber = {"A","2","3","4","5","6","7","8"
					,"9","10","J","Q","K"};
		//integration
		String[] strPu = new String[strFlower.length*strNumber.length];
		for(int i=0,k=0; i<strFlower.length;i++){
			for(int j=0; j<strNumber.length; j++,k++){
				strPu[k] = strFlower[i]+strNumber[j];
			}
		}
		System.out.println(strPu[1-1]+"  "+strPu[5-1]+"  "+strPu[50-1]);
	}
}

Question 4

Statistical Characters

Character array: {a','l','f','m','f','o','b','b','s','n'}

The number of occurrences of each character is counted and printed to the console.

Code implementation, part of the effect as shown in the figure:

Code implementation:
 

public class Test04 {
	public static void main(String[] args) {
		char[] ch = {'a','l','f','m','f','o','b','b','s','n'};
		int[] count = new int[26];
		// Traverse will calculate the number of times each letter appears
		for(int i = 0; i < ch.length; i++){		
			count[ch[i]-97]++;		
		}
		
		// Traversing count arrays to display results
		for(int i = 0; i < count.length; i++){
			if(count[i] != 0){
				System.out.println((char)(i+97)+"--"+count[i]);
			}
		}
	}
}

Topic 5

How many points are counted above the average score?

Define arrays [95, 92, 75, 56, 98, 71, 80, 58, 91, 91].

Code implementation, the effect is shown as follows:

Code implementation:

public class Test05 {
	public static void main(String[] args) {
		
		int[] arr = {95,92,75,56,98,71,80,58,91,91};
		// Total score, average score
		int acount = 0, avge = 0;
		// Number higher than average
		int count = 0;
		for(int i = 0; i < arr.length; i++){
			acount += arr[i];
		}
		avge = acount / arr.length;
		for(int i = 0; i < arr.length; i++){
			if(arr[i] > avge){
				count++;
			}
		}
		System.out.println("higher than average score:"+avge+number has"+count+number");
	}
}

Question 6

Determine whether the element values in an array are symmetrical.

Code implementation, the effect is shown as follows:

Code implementation:

public class Test06 {
	public static void main(String[] args) {
		int[] arr = {1,2,3,5,4,3,2,1};
		int len = arr.length - 1;
		//Hypothetical symmetry
		boolean flag = true;
		//Traversal, closing judgment
		for(int i = 0; i <= len ; i++){
			if(arr[i] != arr[len--]){
				flag = false;
				break;
			}
		}
		for(int i = 0; i < arr.length; i++){
			if(i == 0){
				System.out.print("["+arr[i]);
			}else{
				System.out.print(","+arr[i]);
			}
		}
		System.out.print("]Is it symmetrical?");
		System.out.println(flag?"Symmetric":"Asymmetry");
	}
}

Question 7

Compare the contents of the two arrays to see if they are identical.

Code implementation, the effect is shown as follows:

Code implementation:

public class Test07 {
	public static void main(String[] args) {
		int[] arr1 = {1,2,3,4,5,6,7};
		int[] arr2 = {1,2,3,4,5,6,7};
	
		boolean flag = true;//Consistent assumptions
		if(arr1.length!=arr2.length){
			flag = false;
		}else{
			for (int i = 0; i < arr2.length; i++) {
				if(arr1[i] != arr2[i]){
					flag = false;
					break;
				}
			}
		}
		System.out.println("Is it consistent?" + flag);
	}
}

Question 8

According to the standard answer [ADBCD], each question has a total of 10 points, and each student's final score is calculated.

The four students'answers are: Xiao Shang: [DCBAD] Xiao Si: [ADBCD] Xiao Gu: [ADBCA] Xiao Hao: [ABCDD] gets 2 points for each correct question and outputs the final scores of four students.

Code implementation, the effect is shown as follows:

Code implementation:

package com.atguigu.com;

public class Test08 {
	public static void main(String[] args) {
		char[] answer = {'A','D','B','C','D'};
		
		char[] jue = {'D','C','B','X','D'};
		char[] qing = {'A','D','B','C','D'};
		char[] gu = {'A','D','B','C','A'};
		char[] good = {'A','B','C','D','D'};
		
		String[] str = Xiaojie, Xiaoqing, Xiaogu and Xiaohao.
		
		int[] counts = new int[4];
		for(int i = 0; i < answer.length; i++){
			if(answer[i] == jue[i]){
				counts[0]++;
			}
			if(answer[i] == qing[i]){
				counts[1]++;
			}
			if(answer[i] == gu[i]){
				counts[2]++;
			}
			if(answer[i] == good[i]){
				counts[3]++;
			}
		}
		for(int i = 0; i < counts.length; i++){
			System.out.println("full score 10,"+str[i]+": score:"+counts[i]*2+"score");

		}

	}
}

Question 9

Left parity and right parity

10 Array of integers{26,67,49,38,52,66,7,71,56,87}. Elements are rearranged, all odd numbers are saved to the left of the array, and all even numbers are saved to the right of the array.

Code implementation, the effect is shown as follows:

Code implementation:

package com.atguigu.com;

public class Test09 {
	public static void main(String[] args) {
		int[] arr = {26,67,49,38,52,66,7,71,56,87};
		int left = 0
			,right = arr.length - 1;
		System.out.print("Primitive array:\n[");
		//Output primitive array
		for(int i = 0; i <= arr.length-1; i++){
			if(i == 0){
				System.out.print(arr[i]);
			}else{
				System.out.print(","+arr[i]);
			}
		}
		//Judging odd and even numbers and commutation
		while(left < right){
			if(arr[left] % 2 == 0 && arr[right] % 2 != 0){
				int temp = arr[left];
				arr[left] = arr[right];
				arr[right] = temp;
				
				left++;
				right--;
			}else if(arr[left] % 2 != 0){
				left++;
			}else if(arr[right] % 2 == 0){
				right--;
			}
				
			
		}
		System.out.print("]\n After arrangement:\n[");
		//Exchanged array
		for(int i = 0; i <= arr.length-1; i++){
			if(i == 0){
				System.out.print(arr[i]);
			}else{
				System.out.print(","+arr[i]);
			}
		}
		System.out.print("]");

	}
}

Question 10

Case: Enter the number of students from the keyboard, and the results of the students in this group. Store the results in an array, and then sort them from high to low.

Code implementation:

import java.util.Scanner;

public class Test10 {
	public static void main(String[] args) {		
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter the number of students in this group:");
		int count = scan.nextInt();
		//Define a variable array
		int[] number = new int[count];
		//Input score
		for(int i = 0 ; i < number.length; i++){
			System.out.println("Please enter _____________"+(i+1)+"Achievements of individual colleges");
			number[i] = scan.nextInt();
		}
		//sort
		int max = number[0];
		for(int i = 0; i < number.length ;i++ ){
			for(int j = i; j < number.length; j++){
				if( number[j] > number[i]){
					int temp = number[i];
					number[i] = number[j];
					number[j] = temp;
				}
				
			}			
		}
		for(int i = 0; i < number.length; i++){
			System.out.print(number[i]+" ");
		}
		
	}
}
 

Question 11

Case: Enter the number of students and their names from the keyboard, store their names in an array, and then enter a name from the keyboard to find out if it is in the previous array, and if it exists, display its subscript.

Code implementation:

import java.util.Scanner;

public class Test11 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter the number of students in this group:");
		int count = scan.nextInt();
		String[] names = new String[count];
		for(int i = 0; i < names.length; i++){
			System.out.println("Please enter _____________"+(i+1)+"Name of each student:");			
			names[i] = scan.next();
		}
		System.out.println("Please enter the name you want to find:");
		String isEm = scan.next();
		boolean flag = true;
		for(int i = 0; i < names.length ;i++){
			if(isEm.equals(names[i])){
				System.out.println("The subscripts of the names checked are:"+i);
				flag = false;
				break;
			}
		}
		if(flag){
			System.out.println("No student!");
		}
		 
	}

}

Question 12

Case study: Enter an English word from the keyboard and find out if it exists.'a'word

Code implementation:

import java.util.Scanner;

public class Test12 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter a word:");
		String number = scan.next();
		char[] ch = number.toCharArray();
//		boolean flag = true;
		for(int i = 0; i < ch.length ;i++){
			if('a' == ch[i]){
				System.out.println(ch[i]+"existence");
				break;
			}else if(ch.length-1 == i){  //After traversing, no representative is found.
				System.out.println("a Non-existent");
			}
		}
	}

}

Question 13

Random Verification Code.

A verification code consisting of ten groups of six-bit characters is generated randomly.

Verification codes are composed of upper and lower case letters and numeric characters.

Code implementation:
 

import java.util.Random;

public class Test13 {
	public static void main(String[] args) {
		String[] str = new String[10];
		Random rand = new Random();
		
		for(int i = 0; i < 10; i++ ){
			str[i] = "";
			for(int j = 0; j < 6; j++){
				int num;
				while(true){
					num = rand.nextInt(123);
					if(num >= 48 && num <= 57){
						break;
					}else if(num >= 65 && num <= 90){
						break;
					}else if(num >= 97 && num <= 122){
						break;
					}
				}
				str[i] += (char)num;
			}
			System.out.println("random verification code:"+str[i]);
			
			
		}
	}
}

Question 14

Array weighting

10 Number of integers{9,10,6,6,1,9,3,5,6,4},Scope 1-10,Save it to an array.

Remove duplicates from the array, leaving only one element. The effect is shown as follows:

Code implementation:

public class Test14 {
	public static void main(String[] args) {
		int[] arr = {9,10,6,6,1,9,3,5,6,4};
		//1 Defines the variable count and initializes it to the length of the original array
		
		for(int i = 0; i < arr.length ; i++){
			System.out.print(arr[i]+" ");
		}
		int count = arr.length ;
		//2. Find out if there is a duplicate element, and cover the latter with the duplicate element, then count--
		for(int i = 0; i < count ;i++){
			for(int j = i + 1; j < count ;j++){
				if(arr[i] == arr[j]){
					for(int k = j; k < count-1; k++){
						arr[k] = arr[k+1];
					}
					count --;
				}
			}
		}
		System.out.println();
		//3 Create new arrays
		int[] newArr = new int[count];
		for(int i = 0; i < count; i++){
			newArr[i] = arr[i];
		}
		//4 Copy the elements, copy the count elements in front of arr into the new array
		for(int i = 0; i < newArr.length ; i++){
			System.out.print(newArr[i]+" ");
		}
	}
}

Question 15

Print a 10-line Yang Hui triangle using a two-dimensional array.As shown in the figure:

Code implementation:

public class Test15 {
	public static void main(String[] args) {
		int[][] arr = new int[10][];
		//Initialization
		for(int i = 0;i < arr.length ; i++){
			arr[i] = new int[10];
			
		}
		//assignment
		for(int i = 0; i < arr.length ;i++){
			for(int j = 0; j < i+1 ; j++){
				if( j == 0 || i == j ){
					arr[i][j] = 1; 
				}else{
					arr[i][j] = arr[i-1][j]+arr[i-1][j-1];
				}
			}
		}
		
		//ergodic
		for(int i = 0; i < arr.length ; i++){
			for(int j = 0; j < i + 1 ; j++){
				System.out.print(arr[i][j]+"\t");
			}
			System.out.println();
		}
	}
}

Question 16

Print playing cards. The effect is as follows:

Code implementation:

public class Test16 {
	public static void main(String[] args) {
		String[][] arr = new String[2][];
		
		arr[0] = new String[4];
		arr[1] = new String[13];
		
		arr[0][0] = "Spade";
		arr[0][1] ="Heart";
		arr[0][2] = "Plum blossom";
		arr[0][3] = "Square sheet";
		
		arr[1][0] = "A";
		for(int i = 1; i < 10; i++){
			arr[1][i] = i+1+"";
		}
		arr[1][10] = "J";
		arr[1][11] = "Q";
		arr[1][12] = "K";
		
		for(int i = 0; i < arr[0].length ; i++){
			for(int j = 0; j < arr[1].length ;j++){
				System.out.print(arr[0][i]+arr[1][j]+" ");
			}
			System.out.println();
		}
	}
	
}

 

Question 17

Needs: Keep the grades of each group in the class and make statistics on them.

  1. There are several groups of keyboard input

  2. How many people each group has entered from the keyboard

  3. Enter each student's grades from the keyboard

  4. Statistics of the highest and lowest scores in each group

  5. Statistical average score for each group

  6. Statistics of the highest and lowest marks in the class

  7. Statistics the average score of the whole class

  8. Statistics of the total number of students in the class

The question is a little complicated, but it's cleared up after much thought.

Examples are shown as follows:

Code implementation:

import java.util.Scanner;

public class Test17 {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("Please enter a total of several groups:");
		int club = scan.nextInt();
		int[][] people = new int[club][];
		for(int i = 0; i < club;i++){		
			System.out.println("Please enter _____________"+(i+1)+"How many people?");
			people[i] = new int[scan.nextInt()];
			for(int j = 0; j < people[i].length ;j++){
				System.out.println("Please enter"+(j+1)+"The results of the team members:");
				people[i][j] = scan.nextInt();
			}
		}
		System.out.println("-------------------------");
		//Display the score sheet
		System.out.println("The results of each group are as follows:");
		for(int i = 0; i < people.length ; i++){
			System.out.println("The first"+(i+1)+"Group:");
			for(int j = 0; j < people[i].length ;j++){
				System.out.print(people[i][j]+"\t");			
			}
			System.out.println();
		}
		
		//Calculate the highest score for each group
		int max[] = new int[people.length ];
		//Minimum score for each group
		int min[] = new int[people.length];
		//Total Achievements
		int Account = 0;
		//Average score for each group
		double avg[] = new double[people.length ];
		for(int i = 0; i < people.length ; i++){
			max[i] = people[i][0];
			min[i] = people[i][0];
			//avg[i] = people[i][0];
		}	
		//Number per group
		int count[] = new int[people.length ];
		//Find the maximum, minimum, number, total score of each group
		for(int i = 0; i < people.length ; i++){
			for(int j = 0; j < people[i].length;j++ ){
				if(max[i] < people[i][j]){
					max[i] = people[i][j];				
				}
				if(min[i] > people[i][j]){
					min[i] = people[i][j];
				}
				count[i] += people[i][j];
				Account += people[i][j];
				
			}
		}
		//Total number
		int countNumber = 0;
		for(int i = 0; i < count.length ; i++){
			avg[i] = ((int)((count[i]*1.0) / people[i].length)*100)/100*0.1 ;
			countNumber += people[i].length ;
		}	
		//Total average score
		double countAvge = 0.0;
		countAvge = ((int)((1.0*Account) / countNumber)*100)/100*0.1;
		int maxs = max[0];
		int mins = min[0];
		System.out.println("-------------------------");
		System.out.println("The highest score and the lowest score in each group:");
		//The highest and lowest grade in the class
		for(int i = 0; i < max.length ; i++){
			System.out.println("The first"+(i+1)+"The highest score of the group was:"+max[i]+",Minimum score:"+min[i]
					+",Average:"+avg[i]);
			if(maxs < max[i]){
				maxs = max[i];
			}
			if(mins > min[i]){
				mins = min[i];
			}
			
		}
		System.out.println("The highest grade in the class:"+maxs+",Minimum score:"+mins+
				",Average:"+countAvge+",Total number:"+countNumber);
		
		
	}

}

The short answers (including some basic grammar, algorithms, search, copy, inversion, sorting) will be shared next time. See you next time.

For student number programming, we still have to write more and practice more to master it skillfully, such as looking at a question to know the train of thought and so on! _________.

 

 

Posted by efegue on Fri, 26 Jul 2019 04:33:53 -0700