我正在进行公牛和牛比赛

时间:2013-12-02 03:54:58

标签: java

import java.util.InputMismatchException;
import java.util.Random;
import java.util.Scanner;

public class Bulls{

public static void main(String[] args){

    System.out.println("Welcome to the game of BULLS and COWS."); 
    System.out.println("The objective in this game is for you to guess a 4-digit number.");  
    System.out.println("The computer responds with how close your guess is to the target ");        
    System.out.println("BULLS = # common digits with exact matches and" );
    System.out.println("COWS  = # common digits in wrong position.");

    Random gen= new Random();
    int guess;
    int target= 0;
    while(produceRandomTarget(target= (gen.nextInt(9000) + 1000)));
    String targetStr = target +"";
    boolean guessed = false;
    Scanner input = new Scanner(System.in);
    int guesses = 0;
    do{
        int bulls = 0;
        int cows = 0;
        System.out.print("Guess a 4-digit number with no duplicate digits: ");      

        try{
            guess = input.nextInt();
            if(produceRandomTarget(guess) || guess < 1000) continue;
        }catch(InputMismatchException e){
            do {
                while (!input.hasNextInt()) {
                    System.out.println("Your guess should not contain non-digits");
                    input.next();
                }
                while (!input.hasNextInt()) {
                    System.out.println("Your guess should not contain repeating digits");
                    input.next(); 
                }
                while (!input.hasNextInt()) {
                    System.out.println("Your guess should contain 4 symbols (digits)");
                    input.next(); 
                }
                guess = input.nextInt();
            } while (guess <= 0);
                continue;
        }
        guesses++;
        String guessStr = guess + "a";
        for(int i= 0;i < 4;i++){
            if(guessStr.charAt(i) == targetStr.charAt(i)){
                bulls++;
            }else if(targetStr.contains(guessStr.charAt(i)+"")){
                cows++;
            }
        }
        if(bulls == 4){
            guessed = true;
        }else{
            System.out.println(cows+" Cows and "+bulls+" Bulls.");
        }
    }while(!guessed);
    System.out.println("Congratulations; You Won!!");
    while (guess != target && guesses < 10);

    if(!guessed){
        System.out.println("Congratulations; You Won!!"); 
    }else{

            System.out.println("Sorry; You ran out of guesses!!");
        System.out.println("The correct answer was: " + target);
    }
}

public static boolean produceRandomTarget(int num){
    boolean[] digs = new boolean[10];
    while(num > 0){
        if(digs[num%10]) return true;
        digs[num%10] = true;
        num/= 10;
    }
    return false;
    }
}

我正在使用Java进行Bulls and Cow游戏,但是我修改了它以包含一个介绍并在用户不输入整数时打印消息。当用户在10次尝试中没有得到答案时,以及当用户输入超过4位数或输入重复数字时的消息时,我在打印消息时遇到一些问题。请帮忙,谢谢。

EDIT 对不起,我应该更好地澄清这个程序。这是一个像Mastermind的游戏,你试着猜测一个随机的4位数字。如果你在正确的位置得到一个数字,它将是一个BULL,如果数字一般在那里它将是一个COW。 让我们使用5296例如,如果我猜到4260,则会有1个BULL和1个COW,因为2位于正确的位置,而6位于数字中但不在右侧。   所以我生成了代码,其中有一条欢迎信息然后打印一个提示,要求用户输入一个4位数字。我得到它产生消息“你的猜测不应该包含非数字”所以只有整数应该出现,但我遇到循环问题,以便当有重复的数字时“你的猜测不应该包含重复的数字”打印当您输入4位以上时,应显示“您的猜测应包含4个符号(数字)”。最后,当你错过超过10个猜测并打印信息时,我需要结束游戏。 我有代码,但我无法弄清楚为什么他们在我运行程序时不会出现。

0 个答案:

没有答案