本文目錄一覽:
- 1、java遊戲編程1A2B是一款十分經典的猜數字遊戲,每局開始,計算機都會隨機生成四個數字?
- 2、java猜數字遊戲?
- 3、用java編寫一個猜數字遊戲,
- 4、java猜數字小遊戲。用eclipse寫的
- 5、Java猜數字遊戲
java遊戲編程1A2B是一款十分經典的猜數字遊戲,每局開始,計算機都會隨機生成四個數字?
package com.test;
import java.util.Random;
import java.util.Scanner;
/**
* 我的測試類
*
* @author 劉逸暉
*
*/
public class MyTest {
/**
* 生成不同的正整數隨機數,返回字元串數組
*
* @param count
* 需要生成隨機數的數量
* @param max
* 隨機數的最大值
* @param min
* 隨機數的最小值
* @return 生成的隨機數
*/
private static String[] generateRandomNumber(int count, int min, int max) {
if (count 0 min -1 max min) {
String[] numbers = new String[count];
Random random = new Random();
// 生成隨機數。
for (int i = 0; i numbers.length; i++) {
numbers[i] = min + random.nextInt(max – min) + “”;
}
// 檢查是否存在重複的隨機數。
int equalIndex = areEqual(numbers);
while (equalIndex != -1) {
numbers[equalIndex] = min + random.nextInt(max – min) + “”;
equalIndex = areEqual(numbers);
}
return numbers;
} else {// 參數不合法。
return null;
}
}
/**
* 判斷字元串數組中的元素是否存在相等的
*
* @param array
* 預判斷的數組
* @return 如果數組中有相等的元素,返回其下標;如果數組中沒有相等的元素,或數組為空返回-1
*/
private static int areEqual(String[] array) {
if (array != null array.length 0) {
// 將數組中的每一個成員與其之前的所有成員進行比較,判斷是否有相等的。
for (int current = 0; current array.length; current++) {
// 將當前便利的數組成員與其之前的所有成員進行比較,判斷是否有相等的。
for (int previous = 0; previous current; previous++) {
if (array[current].equals(array[previous])) {
return previous;
}
}
}
}
return -1;
}
/**
* 搜索字元串數組
*
* @param array
* 數組
* @param value
* 預搜索的值
* @return 如果數組中有成員的值與預搜索的值相等返回成員下標,否則返回-1
*/
private static int search(String[] array, String value) {
if (array != null array.length -1 value != null) {
for (int i = 0; i array.length; i++) {
if (array[i].equals(value)) {
return i;
}
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(“歡迎你來到1a2b,輸入n退出,輸入y重新開始”);
System.out.println(“系統會隨機產生4個0到9之間不同的數字,請你來猜”);
System.out.println(“輸出a不僅代表你猜中了,還代表你猜對它的位置了哦!\r\n輸出b則代表你猜中了,但位置不對哦”);
// 開始循環,一次循環代表一局遊戲。一局結束後立刻開啟下一局。
while (true) {
System.out.println(“新的一局開始了!”);
// 產生隨機數。
String[] randomNumbers = generateRandomNumber(4, 0, 9);
Scanner scanner = new Scanner(System.in);
// 創建變數存放輸入記錄。
String[] records = new String[] { “”, “”, “”, “” };
// 創建變數存放ab結果。
String result = “”;
// 請用戶輸入4次值。為什麼請用戶輸入4次?因為數組中有4個成員。
for (int i = 0; i randomNumbers.length; i++) {
// 獲得輸入的值。
String inputValue = scanner.nextLine();
// 判斷是否需要退出。
if (inputValue.equals(“n”) || inputValue.equals(“”)) {
System.out.println(“Goodbye”);
return;
}
// 創建變數定義是否忽略本次輸入。
boolean ignore = false;
// 判斷是否需要重新開始。
if (inputValue.equals(“y”)) {
ignore = true;
i = randomNumbers.length;
}
// 判斷是否重複輸入。
for (String record : records) {
if (inputValue.equals(record)) {
ignore = true;
i–;
System.out.println(“這個值你已經輸入過了哦!\r\n在給你一次機會。”);
continue;
}
}
if (ignore) {
continue;
}
// 對輸入的值進行搜索。
int searchResult = search(randomNumbers, inputValue);
// 如果搜索到了相關的值。
if (searchResult -1) {
// 記錄。
records[i] = inputValue;
// 不僅搜索到了輸入的值,並且位置正確。
if (searchResult == i) {
result = result + “a”;
System.out.println(“a”);
} else {// 搜索到了輸入的值,但位置錯誤。
result = result + “b”;
System.out.println(“b”);
}
} else {// 輸入錯誤。
System.out.println(“這裡沒有這個值哦!\r\n再給你一次機會!”);
i–;
}
}
System.out.println(result);
}
}
}
java猜數字遊戲?
import java.util.Random;
import java.util.Scanner;
/**
* @Author: Cool_Wu
* @Date: 2020-12-01 23:39
*/
public class GuessNumberGame {
static int count = 0;
static int answer = new Random().nextInt(100);
public static void main(String[] args) throws Exception {
System.out.println(“猜數字遊戲開始,該數字是一個0~100之間的整數”);
compareNum();
}
public static void compareNum() throws Exception {
if (count = 10){
System.out.println(“正確答案是:” + answer);
System.out.println(“你太笨了,下次再來吧!”);
return;
}
count++;
int n = receiveNum();
if (n 0){
throw new Exception(“您輸入的數字不符合要求,請重新輸入!”);
}
if (n 99){
throw new Exception(“輸入錯誤,請輸入正確的數字!”);
}
if (n answer){
System.out.println(“太小了,再大一點!”);
compareNum();
}
if (n answer){
System.out.println(“太大了,再小一點!”);
compareNum();
}
if (n == answer){
System.out.println(“恭喜你,猜對了!”);
}
}
public static int receiveNum() {
System.out.println(“請輸入您猜的數字:”);
int n = new Scanner(System.in).nextInt();
return n;
}
}
運行結果
用java編寫一個猜數字遊戲,
package day06;
import java.util.Scanner;
//猜字元遊戲
public class GuessingGame {
//主方法
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int count = 0; //猜錯的次數
char[] chs = generate(); //隨機生成的字元數組
System.out.println(chs); //作弊
while(true){ //自造死循環
System.out.println(“猜吧!”);
String str = scan.next().toUpperCase(); //獲取用戶輸入的字元串
if(str.equals(“EXIT”)){ //判斷str是否是EXIT
System.out.println(“下次再來吧!”);
break;
}
char[] input = str.toCharArray(); //將字元串轉換為字元數組
int[] result = check(chs,input); //對比
if(result[0]==chs.length){ //位置對為5
int score = chs.length*100 – count*10; //一個字元100分,錯一次減10分
System.out.println(“恭喜你猜對了,得分:” + score);
break; //猜對時跳出循環
}else{ //沒猜對
count++; //猜錯次數增1
System.out.println(“字元對:”+result[1]+”個,位置對:”+result[0]+”個”);
}
}
}
//隨機生成5個字元數組
public static char[] generate(){
char[] chs = new char[5];
char[] letters = { ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’,
‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’,
‘W’, ‘X’, ‘Y’, ‘Z’};
boolean[] flags = new boolean[letters.length]; //1.
for(int i=0;ichs.length;i++){
int index;
do{
index = (int)(Math.random()*letters.length); //0到25
}while(flags[index]==true); //2.
chs[i] = letters[index];
flags[index] = true; //3.
}
return chs;
}
//對比隨機數組與用戶輸入的數組
public static int[] check(char[] chs,char[] input){
int[] result = new int[2];
for(int i=0;ichs.length;i++){
for(int j=0;jinput.length;j++){
if(chs[i]==input[j]){ //字元對
result[1]++; //字元對個數增1
if(i==j){ //位置對
result[0]++; //位置對個數增1
}
break;
}
}
}
return result;
}
}
java猜數字小遊戲。用eclipse寫的
import java.util.Scanner;
/**
* Java命令行版 猜數字遊戲
* @author kaifang
*/
public class GuessNum {
public static void main(String[] args)
{
System.out.println(“======猜數字遊戲======\n”);
int answer = (int)(Math.random() * 200 + 1);
Scanner sr = new Scanner(System.in);
while(true) {
System.out.print(“請輸入你猜的數字(1-200):”);
int in = sr.nextInt();
if (in answer) {
System.out.println(“猜大了!\n”);
} else if(in answer){
System.out.println(“猜小了!\n”);
} else {
System.out.println(“恭喜你,才猜對了!!!\n”);
break;
}
}
sr.close();
}
}
Java猜數字遊戲
public static void main(String[] args) {
// TODO 自動生成方法存根
System.out.println(“歡迎進入猜數字遊戲!您只有10次機會!猜的數字在0到100之間”);
Random r = new Random();
int num = r.nextInt(100);
Scanner input = new Scanner(System.in);
int cai;
for (int i = 0; i 10; i++) {
System.out.print(“輸入競猜數字:”);
cai = input.nextInt();
if (cai 0 || cai 100) {
System.out.println(“數字在0到100之間”);
continue;
}
if (cai == num) {
System.out.println(“猜中數字,勝利了”);
break;
} else {
System.out.println(“沒有猜中”);
}
if (i == 9) {
System.out.println(“時間到,競猜失敗”);
}
}
}
原創文章,作者:TWPB,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/132062.html