Unit 1 test questions (4 questions in total)

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 learn;
import java.util.Arrays;
import java.util.Scanner;

public class UnitTesting {
public static void main(String[] args) {
second();
thirdly();
four();
int letters =0;/ /Number of English
int figure=0;//Number of numbers
int spacing=0;//Number of spaces
int rests =0;//Other number
Scanner input = new Scanner(System.in);
System.out.println ("Please enter a line of characters:");
String element=input.next();
int wo=element.length();//Get the length of the input string and use the length as the loop condition
for (int i = 0; i < wo; i++) {
char n=element.charAt(i);//Compare the strings individually
if(n<='9' && n>='0') {
figure++; //The number of statistics number }else if(n<='z'&& n>='a' || n<='Z' && n>='A') {  

letters++;//Count the number of English
}else if(n==' ') {
spacing++;//Count the number of spaces
}else {rests++;}//Count other numbers
}
System.out.println(" The number of English is "+letters);
System.out.println("The number of numbers is "+figure);
System.out.println("The number of spaces is "+spacing);
System.out.println( "The other number is "+rests);

}

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

public static void second() {

Scanner input = new Scanner(System.in);
int arr[] = new int[10];
System.out.println("Please enter ten integers:");
for (int i = 0; i < arr.length ; i++) {//Use the length of the array as the loop condition
arr[i] = input.nextInt();//The input number } Arrays.sort(arr);//Use the Arrays method to sort the array in ascending order System.out .println("The sorted result is:"); for (int i = 0; i <arr.length; i++) { System.out.print( arr[i]+" "); }






}


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

public static void thirdly() {
int[] group = new int[] { 10, 20, 30 };//The length and value of the first array
int[] group2 = new int[] { 40, 50, 60 } ;//The length and value of the second array
int[] group3 = new int[group.length + group2.length]; //The lengths of the two arrays are combined into the length of one array
System.out.print("The first The elements in the array are: ");
for (int i = 0; i < group.length; i++) {
System.out.print(group[i] + " ");
}
System.out.print("\n The elements in the second array are: ");
for (int i = 0; i < group2.length; i++) {
System.out.print(group2[i]+" ");
}
for (int i = 0; i <group.length; i++) {
group3[i]=group[i]; //Assign the value of the first array to the third array
group3[i+3]=group2[i]; // Assign the value of the first array The values ​​of the 2 arrays are assigned to the 3rd array (note that the subscript starts from 3)
}
System.out.print("\nMerge to: ");
for (int i = 0; i < group3.length; i++) {
System.out.print(group3[i]+" ");
} System.out.print("\n逆序为:"); for (int i = 5; i >= 0; i--) { System.out.print(group3[i]+" "); } }
 





Question 4: Use object-oriented thinking to write Java programs. The keyboard receives the names (name) and height (height, in meters) of 5 students ( Student ) entered by the user, and outputs the average height of these students

public static void four() {

double[] arr=new double[5];
String[] name=new String[5];
double totalfheight = 0.0;//initialization of total height
double avgheight=0.0;//initialization of draw height
for (int i = 0; i < arr.length; i++) {
System.out.print("Please enter the name and height of the "+(i+1)+" classmate:");
Scanner input = new Scanner(System.in);
String neme =input.next();
double height=input.nextInt();
arr[i] =height;
totalfheight += arr[i];
avgheight=totalfheight/5;
}System.out.println("The average height is:" +avgheight);
}
}

Guess you like

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