Experiment 7 of "Java Programming" - The use of the random function Random()

Experiment 7 of "Java Programming"

1. Write a program to randomly generate k integers in the interval [m, n], sort the k data in ascending order and output them.

import java.util.Random;
import java.util.Scanner;

public class Num {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		int num = sc.nextInt();
		int array[] = new int[num];
		Random ran = new Random();
		sc.close();
		for(int i = 0;i < array.length;i++) {
    
    
			array[i] = ran.nextInt(30) + 1;
		}
		
		for(int i = 0;i < array.length;i++) {
    
    
			for(int j = 0;j < array.length - 1 - i;j++) {
    
    
				if(array[j] > array[j + 1]) {
    
    
					int temp = array[j];
					array[j] = array[j + 1];
					array[j + 1] = temp;
				}
			}
		}
		
		for(int i = 0;i < array.length;i++) {
    
    
			System.out.println(array[i] + " ");
		}
	}
}

2. There is such a kind of character strings, they are the same number when looking forward and backward, for example: 121, 656, 2332, ABCBA, etc. Such character strings are called palindrome strings. Write a Java program to determine whether the string received from the keyboard is a palindrome.

package homework.test;
import java.util.Scanner;
public class HuiWenString {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		sc.close();
		StringBuffer sb = new StringBuffer(s);
		sb.reverse();
		if(s.equals(sb.toString())) {
    
    
			System.out.println(s + "是一个回文串");
		}
		else {
    
    
			System.out.println(s + "不是一个回文串");
		}
	}
}

3. Use the interface as a parameter, write a calculator, and be able to complete ±*/ operation

(1) Define an interface Compute that contains a method int computer(int n, int m);

package homework.test;

public interface Compute {
    
    
	int computer(int n,int m);
}

(2) Design four classes to implement this interface respectively, and complete the ±*/ operation;

package homework.test;

public class Add implements Compute {
    
    

	@Override
	public int computer(int n, int m) {
    
    
		return n + m;
	}
}
package homework.test;

public class Subtract implements Compute {
    
    

	@Override
	public int computer(int n, int m) {
    
    
		return n - m;
	}
}
package homework.test;

public class Multiply implements Compute {
    
    

	@Override
	public int computer(int n, int m) {
    
    
		return n * m;
	}
}
package homework.test;

public class Divide implements Compute {
    
    

	@Override
	public int computer(int n, int m) {
    
    
		return n / m;
	}

}

(3) Design a class UseCompute, which contains the method:
public void useCom(Compute com, int one, int two)
This method requires the ability to:
1. Use the passed object to call the computer method to complete the operation
2. Output the result of the operation

package homework.test;

public class UseCompute {
    
    
	public static void useCom(Compute com,int one,int two) {
    
    
		System.out.println("调用的类为:"+com.getClass().getName()+" 得到的结果为:"+com.computer(one, two));
	}
}

(4) Design a test class and call the method useCom in UseCompute to complete the ±*/ operation

package homework.test;

public class ComputeTest {
    
    
	public static void main(String[] args) {
    
    
		double ans;
		Compute computer;
		computer = new Add();
		UseCompute.useCom(computer,20,5);
		computer = new Subtract();
		UseCompute.useCom(computer,20,5);
		computer = new Multiply();
		UseCompute.useCom(computer,20,5);
		computer = new Divide();
		UseCompute.useCom(computer,20,5);
	}
}

4. Design a 13-digit ISBN book number verification program. The calculation rule of the check code of the 13-digit ISBN is as follows: the first 12 digits are multiplied by 1 and 3 in turn, then the remainder of their sum is divided by 10, and finally the remainder is subtracted from 10 to obtain the check code ( last digit). If the remainder is 0, the checksum is 0.
For example, ISBN: 9787302222224, its check code calculation method is as follows:
9x1+7x3+8x1+7x3+3x1+0x3+2x1+2x3+2x1+2x3+2x1+2x3
= 9+21+8+21+3+0 +2+6+2+6+2+6
= 86 86%10=6 10-6=4 So, in a 13-digit ISBN, the checksum of this book should be 4. Write an application program ISBNVerification.java, so that the program can realize the following functions: input a certain ISBN book number in China from the keyboard, the program judges whether the input ISBN book number meets the standard book number, and if the input book number meets the standard, then output "conforms to the standard" , otherwise output "does not meet standard".

package homework.test;

import java.util.Scanner;
public class ISBNVerification {
    
    
	public static void main(String[] args) {
    
    
		String s;
		Scanner sc = new Scanner(System.in);
		s = sc.next();
		sc.close();
		int num = 0;
		for(int i = 0;i < 12; i++)
		{
    
    
			if(i % 2 == 0)
			{
    
    
				num += (s.charAt(i)-'0');
			}
			else num += (s.charAt(i)-'0')*3;
		}
		if(num % 10 == 0) System.out.println("校验码为0");
		else System.out.println("校验码为"+(10-(num%10)));
	}
}

5. Input a positive integer from the console, and program to output the sum of the digits of the positive integer.

package homework.test;

import java.util.Scanner;
public class GetSum {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入一个整数:");
		long input = sc.nextLong();
		sc.close();
		System.out.println(getSum(input));
	}
	
	public static long getSum(long num) {
    
    
		long units;
		long sum = 0;
		while(num != 0) {
    
    
			units = num % 10;
			sum += units;
			num = (num - units) / 10;
		}
		return sum;
	}
}

6. Use a computer to simulate 30,000 dice rolls, and write a program to count the number of times 1, 2, 3, 4, 5, and 6 appear.

package homework.test;

public class Statistics {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("掷骰子30000次,各点值出现的次数:\n");
		int m;
		int score[][] = {
    
    {
    
    1,0},{
    
    2,0},{
    
    3,0},{
    
    4,0},{
    
    5,0},{
    
    6,0}};
		for(int k = 0;k <30000;k++) {
    
    
			m = (int)(1 + Math.random() * 6);
			score[m - 1][1]++;
		}
		for(int x = 0;x < 6;x++) {
    
    
			for(int y = 0;y < 2;y++) {
    
    
				System.out.println(score[x][y]);
			}
			System.out.println("\n");
		}
	}
}

7. Determine the type of file entered by the user according to the file name (full name with file suffix) entered by the user. In order to simplify the problem, we only consider the following types here:
(1) .txt, .doc: text files; (2) .jpeg, .jpg, .bmp, .png, .gif: image files;
(3) .wmv, .avi, .rmvb: video files; (4).mp3: audio files; (5).java: java class files.

package homework.test;

import java.util.Scanner;
public class FileType {
    
    
	public static void main(String[] args) {
    
    
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入文件名:");
		String str = sc.next();
		fileType(str);
		sc.close();
	}
	public static void fileType(String fileName) {
    
    
		if(fileName == null) {
    
    
			fileName = "文件名为空!";
			System.out.println(fileName);
		}
		else {
    
    
			String fileType = fileName.substring(fileName.lastIndexOf(".")+1,fileName.length()).toLowerCase();
			String img[] = {
    
    ".jpeg",".jpg",".bmp",".png",".gif"};
			for(int i = 0;i < img.length;i++) {
    
    
				if(img[i].equals(fileName)) {
    
    
					System.out.println("图片");
				}
			}
			String document[] = {
    
    ".txt",".doc"};
			for(int i = 0;i < document.length;i++) {
    
    
				if(document[i].equals(fileName)) {
    
    
					System.out.println("文档");
				}
			}
			String video[] = {
    
    ".wmv",".avi",".rmvb"};
			for(int i = 0;i < video.length;i++) {
    
    
				if(video[i].equals(fileName)) {
    
    
					System.out.println("视频");
				}
			}
			String music[] = {
    
    ".mp3"};
			for(int i = 0;i < music.length;i++) {
    
    
				if(music[i].equals(fileName)) {
    
    
					System.out.println("音乐");
				}
			}
			String java[] = {
    
    ".java"};
			for(int i = 0;i < java.length;i++) {
    
    
				if(java[i].equals(fileName)) {
    
    
					System.out.println("java");
				}
			}
		}
	}
}

8. Write a program to realize the following specific functions: a guessing game between human and computer - rock, scissors, and cloth.
Rules: <Rock-Scissors>: Rock wins; <Scissors-Paper>: Scissors wins; <Paper-Rock>: Paper wins; when the punches of the human and the computer are the same, a tie is displayed.

package homework.test;

import java.util.Scanner;
public class Game {
    
    
	public static void main(String[] args) {
    
    
		while(true) {
    
    
			System.out.println("******欢迎进入猜拳游戏******");
			System.out.println("请出拳:(1剪刀,2石头,3布)");
			Scanner sc = new Scanner(System.in);
			int person = sc.nextInt();
			int computer = (int)(Math.random()*3 + 1);
			String per = "用户";
			String com = "电脑";
			
			switch(person) {
    
    
			case 1:
				per = "剪刀";
				break;
			case 2:
				per = "石头";
				break;
			case 3:
				per = "布";
				break;
			}
			
			switch(computer) {
    
    
			case 1:
				com = "剪刀";
				break;
			case 2:
				com = "石头";
				break;
			case 3:
				per = "布";
				break;
			}
			
			if(person == 1 && computer == 2 || person == 2 && computer == 3 || person == 3 && computer == 1) {
    
    
				System.out.println("你出的是("+per+")       电脑出的是("+com+")");
				System.out.println("【你输了!继续加油吧!】");
			}
			else if(person == computer) {
    
    
				System.out.println("你出的是("+per+")       电脑出的是("+com+")");
				System.out.println("【平局,请继续。】");
			}
			else {
    
    
				System.out.println("你出的是("+per+")       电脑出的是("+com+")");
				System.out.println("【恭喜你赢了!】");
				System.out.println("【你终于战胜了电脑,游戏结束!】");
				break;
			}
		}
	}
}

Guess you like

Origin blog.csdn.net/mercury8124/article/details/129252118
Recommended