java basic test questions

 The first question: write a Java program, the function is: input a line of characters, respectively count and output the number of English letters, spaces, numbers and other characters in the string.

package cn.test.input;

import java.util.Scanner;


public class Test {

	public static void main(String[] args) {
		// The first question: write a Java program, the function is: input a line of characters, respectively count and output the number of English letters, spaces, numbers and other characters in the string.
		System.out.println("Please enter a line of text: ");
		Scanner input = new Scanner(System.in);
		String aWord = input.nextLine();
		int len ​​= aWord.length(); //Get the length of the string through the length method, which is used for the loop condition of the comparison
		int numCount = 0; //initialize the number of numbers
		int abcCount = 0; //Initialize the number of English letters
		int spaceCount = 0; //Initialize the number of spaces
		int otherCount = 0; //initialize the number of other characters
		for (int i = 0; i < len; i++) {
			char s = aWord.charAt(i); //Read a single character in String
			if(s >= '0' && s <= '9') { // Judge the number
				numCount++;
			}else if((s>='a'&& s<='z') || (s>='A'&& s<='Z')) {  //判断字母
				abcCount++;
			}else if(s == ' ') { //Check for spaces
				spaceCount++;
			}else { //other characters
				otherCount++;
			}
		}
		System.out.println("Number of numbers: "+numCount);
		System.out.println("Number of English letters: "+abcCount);
		System.out.println("Number of spaces: "+spaceCount);
		System.out.println("Number of other characters: "+otherCount);
	}
}

The second question: write a Java program, the keyboard receives 10 integers, saves them in an array, sorts the 10 integers from small to large and outputs them.

package cn.test.input;

import java.util.Scanner;

public class Test2 {

	public static void main(String[] args) {
		/*
		 * Write a Java program, the keyboard receives 10 integers and saves them in an array,
		 * Sort these 10 integers from small to large and output.
		 */
		int arr[] = new int [10]; //Define an array of length 10 to accept 10 integers
		Scanner input = new Scanner(System.in);
		System.out.println("Please enter 10 integers: ");
		for (int i = 0; i < arr.length; i++) { //The loop uses the keyboard to input 10 integers to assign values ​​to the array
			int num = input.nextInt();
			arr[i] = num;
		}
		for (int i = 0; i < arr.length-1; i++) { //sort by bubble sort (from small to large)
			for (int j = 0; j < arr.length-1-i; j++) {
				if (arr[j]>arr[j+1]) {
					int temp = arr[j];
					arr[j] = arr[j+1];
					arr[j+1] = temp;
				}
				
			}
		}
		System.out.println("The sorting result of 10 numbers is: ");
		for (int i = 0; i < arr.length; i++) { //The loop outputs the sorted effect
			System.out.print(arr[i]+", ");
		}
	}

}

Question 3: In a Java program, define two arrays, first combine the two arrays into a new array, and then arrange all the elements in the new array in reverse order

package cn.test.input;

import java.util.Arrays;

public class Test3 {

	public static void main(String[] args) {
		/*
		 * In a Java program, define two arrays, first merge the two arrays into a new array,
		 * Then put all the elements in the new array in reverse order
		 */
		int arr[] = new int [] {10,20,30};
		int arr2[] =new int [] {40,50,60};
		int arrsum[] = new int[arr.length+arr2.length]; //Create an array whose length is the sum of the lengths of these 2 arrays to receive
		System.out.print("The element in the first array is: ");
		for (int i = 0; i <arr.length; i++) { //loop to output the first array and assign a value to the third array
			System.out.print(arr[i]);
			if (i<=1) {
				System.out.print(",");
			}
			arrsum[i] =arr[i];
		}
		System.out.print("\nThe elements in the second array are: ");
		for (int i = 0; i <arr2.length; i++) { //loop through the second array
			System.out.print(arr2[i]);
			if (i<=1) {
				System.out.print(",");
			}
		}
		for (int j = arrsum.length-1; j >= arr.length; j--) { //The loop assigns the last 3 elements of the third array
			arrsum[j] =arr2[j-3];
		}
		System.out.print("\nThe two arrays are merged into: ");
		for (int i = 0; i < arrsum.length; i++) { //loop through the merged array
			System.out.print(arrsum[i]);
			if (i<=4) {
				System.out.print(",");
			}
		}
		for (int i = 0; i < arrsum.length-1; i++) { //Sort by bubble sort (large to small)
			for (int j = 0; j < arrsum.length-1-i; j++) {
				if (arrsum[j]<arrsum[j+1]) {
					int temp = arrsum[j];
					arrsum[j] = arrsum[j+1];
					arrsum[j+1] = temp;
				}
				
			}
		}
		System.out.print("\nAfter reverse order: ");
		for (int i = 0; i < arrsum.length; i++) { //loop to output the merged array
			System.out.print(arrsum[i]);
			if (i<=4) {
				System.out.print(",");
			}
		}
	}

}

Question 4: Write Java programs. The keyboard receives the names (name) and height (height, in meters) of five students (Student) entered by the user, and outputs the average height of these students. Requirements: Implement these functions using loops and arrays.
package cn.test.input;

import java.util.Scanner;

public class Test4 {

	public static void main(String[] args) {
		/*
		 * 4. Write Java programs. The keyboard receives the names (name) and height (height, in meters) of 5 students (Student) entered by the user,
		 * Output the average height of these students. Requirements: Implement these functions using loops and arrays.
		 */
		Scanner input = new Scanner(System.in);
		double arr [] = new double [5]; //Define an array of length 5 to store height
		String names[] = new String [5]; //Define an array of length 5 to store names
		String name = " "; //Initialize name
		double height = 0.0; //initialize height
		double sum = 0.0; //initialize height and
		double avg = 0.000; //Initialize the average height
		for (int i = 0; i < arr.length; i++) { //loop using the keyboard to add elements to the height array
			System.out.print("Please enter the name and height of the "+(i+1)+" classmate: ");
			name = input.next();
			height = input.nextDouble();
			arr[i] = height;
			names[i] = name;
			sum+=arr[i]; //Calculate the height and
		}
		avg = sum /5.00;
		System.out.println("The average height is "+avg+"m.");
	}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325940551&siteId=291194637