java Chapter 1 exercise

Keywords: Java Back-end

5. Write a Java application that outputs all the Greek letters.

Tip: Find the first Greek letter α Encoding in Unicode, last Greek letter ω Encoding in Unicode. And then loop through all the characters in this range to get the Greek alphabet

public class helloworld {
	public static void main(String[] args){
		for (int i = 945; i <= 969; i ++){
			System.out.print((char)i);
		}
    }
}

  6. Enter the integer n from the keyboard, calculate the sum of 1~n and output it.

import java.util.Scanner;
public class helloworld {
	public static void main(String[] args){
		Scanner reader=new Scanner(System.in);
		int n=reader.nextInt();
		int sum=0;
		for (int i = 1; i <= n; i ++){
			sum=sum+i;
		}
		System.out.print(sum);
	}
}

7. Write a Java application that defines array A and assigns values to output all elements of A. Then define array b, copy all or part of the elements of array a into array b, modify the values of array elements through array b, and output all the elements of array a, and compare the results of two outputs.

Tip: Array replication can use the arraycopy method of the System class, which is prototyped as public static void arraycopy(Object)   Source, int   SrcIndex, Object   Dest, int   DestIndex, int   Length), in the parameter, source denotes the source array, srcIndex denotes the location of the elements in the source array that start copying, dest denotes the target array, destIndex denotes where to assign values when copying to the target array, and length denotes the number of elements that are copied. The usage statement for this method is:

System.arraycopy(a,3,b,0,4)//argument is just an example and can be modified as required

import java.util.Arrays;
public class hello1 {
	public static void main(String[] args){
		int[] a=new int[4];
		int[] b=new int[4];
		a[1]=1;a[2]=2;a[0]=0;a[3]=9;
		System.out.println(Arrays.toString(a));
		System.arraycopy(a,0,b,0,4);
		b[1]=123;
		System.out.println(Arrays.toString(a));
	}
}

8. Count the number of occurrences of each letter

     (1) Randomly generate 100 lowercase letters and place them in an array of characters, outputting all the elements in the array.

    (2) Count the number of occurrences of each letter in the above array. To achieve this, you can create an integer array with 26 elements, each holding the number of occurrences of each letter. Output each character and the number of times it occurs.

     Tip: (char) ('a'+ Math.random() * ('z' -'a'+ 1))    Can randomly generate a lowercase letter

import java.util.Arrays;
public class hello1 {
	public static void main(String[] args){
		int[] a=new int[26];
		char[] w=new char[100];
		for(int i=0;i<100;i++){
			w[i]=(char)('a' + Math.random() * ('z' - 'a' + 1));
		}
		for(int i=0;i<100;i++){
			int q;
			q=Integer.valueOf(w[i])-97;
			a[q]=a[q]+1;
		}
		System.out.println(Arrays.toString(w));
		System.out.println(Arrays.toString(a));

}
}

9. Write a Java application that performs the following functions:

     (1) The program randomly assigns customers an integer between 1 and 100.

     (2) Users enter their own guesses.

     (3) The program returns prompt information, which is "guessed big", "guessed small", or "guessed right".

     (4) Users can re-enter guesses based on the prompt until the prompt is "guessed right".

Tip: The program template is given below and can be completed with reference to the program template.

import java.util.Scanner;
import java.util.Random;
public class hello1 {
    public static void main(String args[]){
        Scanner reader=new Scanner(System.in);
        Random random=new Random();
        System.out.printf("Give you a 1~100 Between integers, guess this number\n");
        int realNumber=random.nextInt(100)+1;
        int yourGuess=0;
        System.out.print("Enter your guess:");
        yourGuess=reader.nextInt();
        while(yourGuess!=realNumber)
        {
            if(yourGuess>realNumber)
            {
                System.out.print("Enter your guess:");
                yourGuess=reader.nextInt();
            }
            else if(yourGuess<realNumber)
            {
                System.out.print("Make a small guess and enter your guess:");
                yourGuess=reader.nextInt();
            }
        }
        System.out.printf("You guessed it right!\n");
    }
}

Posted by stupid girl! on Fri, 12 Nov 2021 12:54:09 -0800