猜数字小游戏(JAVA)
生活随笔
收集整理的這篇文章主要介紹了
猜数字小游戏(JAVA)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
猜數字小游戲
- 題目描述
- 代碼
- 運行效果
- 新增功能
- 思路
- 代碼
- 運行效果
題目描述
猜數字(又稱 Bulls and Cows )是一種古老的的密碼破譯類益智類小游戲,起源于20世紀中期,一般由兩個人或多人玩,也可以由一個人和電腦玩。通常由兩個人玩,一方出數字,一方猜。出數字的人要想好一個沒有重復數字的4個數,不能讓猜的人知道。猜的人就可以開始猜。每猜一個數字,出數者就要根據這個數字給出nAmB,其中A前面的數字n表示數字正確且位置也正確的數的個數,而B前的數字m表示數字正確但位置不正確的數的個數。如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個5的位置對了,記為1A,而3和4這兩個數字對了,而位置沒對,因此記為 2B,合起來就是 1A2B。接著猜的人再根據出題者的幾A幾B繼續猜,直到猜中(即 4A0B)為止。
程序要求:1、滿足題意 2、輸入數字的合法性3、輸出總猜測次數
代碼
package Practice; // 猜數字 (Bulls and cows) import java.util.Scanner;public class Day0322 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// 產生0000~9999的隨機數double r = Math.random();int res = (int)(r * 8999 + 1000);int flag = 0;// 合法性檢查,判斷存在重復數字while(flag == 0){int[] check = new int[10];for(int i = 0; i < 10; i ++ ) check[i] = 0;check[res / 1000] += 1;check[(res / 100) % 10] += 1;check[(res / 10) % 10] += 1;check[res % 10] += 1;for(int i = 0; i < 10; i ++ )if(check[i] >= 2) {r = Math.random();res = (int)(r * 8999 + 1000);flag = 0;break;}else flag = 1;}// 0000~9999System.out.println("答案: " + res);int input = -1;int idx = 0;int times = 0;while(input != res){System.out.print("請輸入你猜的數字: ");input = scanner.nextInt();int inputcopy = input;if(input < 0){System.out.println("您輸入的數字不是四位數!");times ++;continue;}int t = 0;// 輸入數字為4位數,合法性檢查while(inputcopy != 0){inputcopy /= 10;t ++;}if(t != 4){System.out.println("您輸入的數字不是四位數!");times ++;continue;}int n = 0, m = 0;// nAmBif(input == res) break;// 輸入的各個位數int[] a = new int[4];a[0] = input / 1000;a[1] = (input / 100) % 10;a[2] = (input / 10) % 10; a[3] = (input) % 10;// 答案的各個位數int[] ans = new int[4];ans[0] = res / 1000;ans[1] = (res / 100) % 10;ans[2] = (res / 10) % 10; ans[3] = (res) % 10;for(int i = 0; i < 4; i ++){if(ans[i] == a[i]) n += 1; // A的數量for(int j = 0; j < 4; j ++){// B的數量if(ans[j] == a[i] && j != i) m += 1;}}System.out.print((++ idx) + ": " + n + "A" + m + "B");System.out.println();times ++;}if(input == res){times ++;System.out.println("4A0B");System.out.println("你很厲害啊!");System.out.println("猜測次數: " + times);}}}運行效果
新增功能
思路
nAmB中,A的數量可直接遍歷字符串的每個位置
若input.charAt(i) == target.charAt(i)則n ++ ;
否則,分別對兩個字符串計數出現的數字數量,需要分別開一個長度為10的數組。
由于是匹配問題,同一個數字在兩個字符串中若出現次數不同,則只能取最小。累加即得m。
代碼
// 猜數字 (Bulls and cows) import java.util.Scanner;public class Main {// 判斷是否為純數字public static boolean check(String str){for(int i = 0; i < str.length(); i ++ ){if(str.charAt(i) >= '0' && str.charAt(i) <= '9') continue;else return false;}return true;}public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// 產生0000~9999的隨機數double r = Math.random();int res = (int)(r * 9999);String target = String.format("%04d", res);// 0000~9999System.out.println("答案: " + target);String input = "";int idx = 0;int times = 0;while(!input.equals(target)){System.out.print("請輸入你猜的4位數字: ");input = scanner.nextLine();if(input.length() != 4) {System.out.println("您輸入的數字長度不是四位~");continue;}if(!check(input)) {System.out.println("您輸入了非數字~");continue;}int n = 0, m = 0;// nAmBif(input.equals(target)) break;// 輸入錯誤時計算n, mint[] cntN = new int[10];int[] cntM = new int[10];for(int i = 0; i < target.length(); i ++ ){if(input.charAt(i) == target.charAt(i)) n ++ ; // 數字對且位置對else{cntN[target.charAt(i) - '0'] ++ ;cntM[input.charAt(i) - '0'] ++ ;}}for(int i = 0; i < 10; i ++ ) m += Math.min(cntN[i], cntM[i]);System.out.print((++ idx) + ": " + n + "A" + m + "B");System.out.println();}if(input.equals(target)){idx ++;System.out.println("4A0B");System.out.println("你很厲害啊!");System.out.println("猜測次數: " + idx);}}}運行效果
總結
以上是生活随笔為你收集整理的猜数字小游戏(JAVA)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我谈网络扫描 -- 之三
- 下一篇: i78700k配什么显卡好_i7 870