java實(shí)現(xiàn)簡(jiǎn)單的猜數(shù)字小游戲
本文實(shí)例為大家分享了java實(shí)現(xiàn)簡(jiǎn)單的猜數(shù)字的具體代碼,供大家參考,具體內(nèi)容如下
題目描述:
猜數(shù)字(又稱 Bulls and Cows )是一種古老的的密碼破譯類益智類小游戲,起源于20世紀(jì)中期,一般由兩個(gè)人或多人玩,也可以由一個(gè)人和電腦玩。通常由兩個(gè)人玩,一方出數(shù)字,一方猜。出數(shù)字的人要想好一個(gè)沒有重復(fù)數(shù)字的4個(gè)數(shù),不能讓猜的人知道。猜的人就可以開始猜。每猜一個(gè)數(shù)字,出數(shù)者就要根據(jù)這個(gè)數(shù)字給出nAmB,其中A前面的數(shù)字n表示數(shù)字正確且位置也正確的數(shù)的個(gè)數(shù),而B前的數(shù)字m表示數(shù)字正確但位置不正確的數(shù)的個(gè)數(shù)。如正確答案為 5234,而猜的人猜 5346,則是 1A2B,其中有一個(gè)5的位置對(duì)了,記為1A,而3和4這兩個(gè)數(shù)字對(duì)了,而位置沒對(duì),因此記為 2B,合起來就是 1A2B。接著猜的人再根據(jù)出題者的幾A幾B繼續(xù)猜,直到猜中(即 4A0B)為止。
程序要求:
1、滿足題意 2、輸入數(shù)字的合法性3、輸出總猜測(cè)次數(shù)
代碼
package Practice;
// 猜數(shù)字 (Bulls and cows)
import java.util.Scanner;
public class Day0322 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 產(chǎn)生0000~9999的隨機(jī)數(shù)
double r = Math.random();
int res = (int)(r * 8999 + 1000);
int flag = 0;
// 合法性檢查,判斷存在重復(fù)數(shù)字
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~9999
System.out.println("答案: " + res);
int input = -1;
int idx = 0;
int times = 0;
while(input != res)
{
System.out.print("請(qǐng)輸入你猜的數(shù)字: ");
input = scanner.nextInt();
int inputcopy = input;
if(input < 0)
{
System.out.println("您輸入的數(shù)字不是四位數(shù)!");
times ++;
continue;
}
int t = 0;
// 輸入數(shù)字為4位數(shù),合法性檢查
while(inputcopy != 0)
{
inputcopy /= 10;
t ++;
}
if(t != 4)
{
System.out.println("您輸入的數(shù)字不是四位數(shù)!");
times ++;
continue;
}
int n = 0, m = 0;// nAmB
if(input == res) break;
// 輸入的各個(gè)位數(shù)
int[] a = new int[4];
a[0] = input / 1000;a[1] = (input / 100) % 10;
a[2] = (input / 10) % 10; a[3] = (input) % 10;
// 答案的各個(gè)位數(shù)
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的數(shù)量
for(int j = 0; j < 4; j ++){// B的數(shù)量
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("猜測(cè)次數(shù): " + times);
}
}
}
運(yùn)行效果

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring核心容器之ApplicationContext上下文啟動(dòng)準(zhǔn)備詳解
這篇文章主要介紹了Spring核心容器之ApplicationContext上下文啟動(dòng)準(zhǔn)備詳解,ApplicationContext 繼承自 BeanFactory ,其不僅包含 BeanFactory 所有功能,還擴(kuò)展了容器功能,需要的朋友可以參考下2023-11-11
Java使用Spire.Presentation for Java將PPT轉(zhuǎn)換為PDF
文章介紹Java開發(fā)者使用Spire.PresentationforJava將PPT轉(zhuǎn)換為PDF,支持跨平臺(tái)兼容、PDF/A標(biāo)準(zhǔn)、加密、隱藏幻燈片和自定義頁面大小等高級(jí)功能,滿足文檔分發(fā)、歸檔與安全需求2025-10-10
Mybatis-Plus的多數(shù)據(jù)源你了解嗎
這篇文章主要為大家詳細(xì)介紹了Mybatis-Plus的多數(shù)據(jù)源,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
HttpClient的RedirectStrategy重定向處理核心機(jī)制
這篇文章主要為大家介紹了HttpClient的RedirectStrategy重定向處理核心機(jī)制源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
SpringBoot整合Redis的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot整合Redis的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

