Java-static methods

1. The computer randomly generates a number between [1, 100]

(1) Every time after comparing the player’s input with a random number, the user’s input value is too high to prompt the player too high, the user’s input value is too small to prompt the player too small, and the user guesses and prompts bingo;

(2) One game termination condition: a successful guess or failure to guess more than 5 times. Output "You win!" if you guess, and output "You lose!" if you don't guess;

(3) Every time the game is terminated, the user is prompted "Do you want to continue the game? (Y/N)", "Yes" to start a new game, "No" to exit the game;

(4) If the user enters 0, it will output "User terminated the game" to terminate the game and exit;

(5) Design static methods (static methods) in the class to achieve the above functions, and the number of methods is unlimited.

public static void main(String[] args) {
    
    
		int a = 0;
		while (true) {
    
    
			if (a == 1) {
    
    
				System.out.println("用户终止游戏!");
				break;
			}
			System.out.println("是否开始游戏(Y/N)");
			@SuppressWarnings("resource")
			Scanner sc = new Scanner(System.in);
			char input = sc.nextLine().charAt(0);
			if (input == 'Y') {
    
    
				System.out.println("开始游戏!");
				for (int i = 0; i < 5;) {
    
    
					@SuppressWarnings("resource")
					Scanner sc2 = new Scanner(System.in);
					// 接收数据
					System.out.println("请输入你要猜的数据(1-100):");
					int guessNumber = sc2.nextInt();
					if (guessNumber == 0) {
    
    
						a = 1;
						break;
					} else {
    
    
						setRandom(guessNumber, i);
						i++;
					}
					if (i == 5) {
    
    
						System.out.println("You lose!");
					}
				}

			} else if (input == 'N') {
    
    
				System.out.println("退出游戏!");
				break;
			} else {
    
    
				System.out.println("请输入正确的符号!");
			}
		}
	}

public static void setRandom(int guessNumber, int i) {
    
    
		int number = (int) Math.floor(Math.random() * 10);// 生成0-100的随机数
		if (number != 0) {
    
    
			if (guessNumber > number) {
    
    
				System.out.println("您猜的数字" + guessNumber + "\ntoo hight" + ",您还剩" + (4 - i) + "次机会");
				i++;
			} else if (guessNumber < number) {
    
    
				System.out.println("您猜的数字" + guessNumber + "\nto small" + ",您还剩" + (4 - i) + "次机会");
				i++;
			} else {
    
    
				System.out.println("You Win!");
				return;
			}
		}
	}

2. Randomly generated array composed of 20 elements

The array elements are randomly generated using 0-9 numbers, az and AZ.

public static void main(String[] args) {
    
    
		int[] a = new int[20];
		int count = 0;
		while (count <= 19) {
    
    
			int t = (int) (Math.random() * 1000);// 抽取的数值小于char类型的“z”
			if ((t >= 0 & t <= 9) | (t >= 65 & t <= 90) | (t >= 97 & t <= 122)) {
    
    
				a[count] = t;
				count++;
			}
		}
		for (int i = 0; i < 20; i++) {
    
    
			if (a[i] >= 0 & a[i] <= 9)
				System.out.print(a[i] + "\t");
			else
				System.out.print((char) a[i] + "\t");
			//ASCII
		}
	}

Guess you like

Origin blog.csdn.net/GodOuO/article/details/108759567