This article describes the Java-implemented number guessing game. Share it for your reference, as follows:
Here is a Java language guessing code, the process is as follows:
For example: The generated is 0369 and the user input is 0396, which displays 2A2B. The two positions are correct and the numbers are correct, and the other two are correct and the positions are incorrect.
Java code:
package com.example.test;import java.util.Random;import java.util.Scanner;public class NumberCode { int[] Nums = new int[4]; int[] inputNumsArray = new int[4]; int difficultyLevel; int difficulty; int aA = 0; int bB = 0; String numberStr = ""; String str = ""; /** * Generate random numbers*/ public int[] randNums(int n) { for (int i = 0; i < Nums.length; i++) { Random ran = new Random(); int a = ran.nextInt(10); if (i - 1 != -1) { for (int j = 0; j < i; j++) { if (a == Nums[j]) { i--; break; } else { Nums[i] = a; } } } else { Nums[i] = a; } } return Nums; } /** * Select game difficulty*/ public int selectLevel() { // Accept a number // 1: Easy can guess 12 times // 2: Common can guess 9 times // 3: Hard You can guess 7 times @SuppressWarnings("resource") Scanner scan = new Scanner(System.in); System.out .println("Please select the difficulty coefficient (input number), 1: Easy can guess 12 times; 2: Common can guess 9 times; 3: Hard can guess 7 times"); difficulty = scan.nextInt(); switch (difficulty) { case 1: difficultLevel = 12; break; case 2: difficultLevel = 9; break; case 3: difficultLevel = 7; break; default: break; } return difficultLevel; } /** * Accept the number input by the user*/ public int[] inputNums(int n) { @SuppressWarnings("resource") Scanner scan = new Scanner(System.in); int b = scan.nextInt(); for (int i = 0; i < inputNumsArray.length; i++) { int c = (int) ((int) b / Math.pow(10, 3 - i)); inputNumsArray[i] = c; b = (int) (b - c * Math.pow(10, (3 - i))); } return inputNumsArray; } /** * Method of number comparison*/ public String compare(int[] answer, int[] inputs) { for (int i = 0; i < answer.length; i++) { if (inputs[i] == answer[i]) { aA += 1; continue; } else { for (int j = 0; j < answer.length; j++) { if (inputs[i] == answer[j]) { bB += 1; } } } } str = "" + aA + "A " + bB + "B "; return str; } /** * The entire game process code*/ public void play() { randNums(4); for (int i = 0; i < Nums.length; i++) { numberStr = numberStr + Nums[i]; } selectLevel(); System.out.println("You selected the difficulty coefficient:" + difficulty + "Total:" + difficultyLevel + "Times of chance."); for (int i = 0; i < difficultyLevel; i++) { inputNums(4); int chanceNums = difficultyLevel - i - 1; compare(Nums, inputNumsArray); if (aA != 4) { if (aA == 0) { System.out.println("The chances are used up, the answer is:" + numberStr); break; } else { System.out.println(str + "You still have" + chanceNums + "Time chance"); } aA = 0; bB = 0; } else if (aA == 4) { System.out.println("Congratulations, you answered correctly"); break; } } } public static void main(String[] args) { NumberCode a = new NumberCode(); a.play(); }}Running results:
Please select the difficulty coefficient (enter the number), 1: Easy can guess 12 times; 2: Common can guess 9 times; 3: Hard can guess 7 times
1
You chose the difficulty coefficient: 1 There are 12 chances in total.
0123
0A 2B You have 11 chances
2345
2A 0B You have 10 more chances
5678
2A 0B You have 9 more chances
7890
0A 2B You have 8 more chances
2378
Congratulations, you've got the answer right
The operation effect is as follows:
For more information about Java algorithms, readers who are interested in this site can view the topics: "Summary of Java Mathematical Operation Skills", "Tutorials of Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.