如何从输入轮换中删除用户?

时间:2018-11-11 23:21:08

标签: java

我正在尝试在Java上进行多人随机数游戏。

我已经成功构建了该游戏的单人游戏版本,但是我正在努力弄清楚如何跳过在多人游戏设置中正确猜出的用户。

这个想法是,每个用户将轮流猜测与他们的订单相对应的数组中存储的各自编号。

如果所有用户都不对,它将再次循环返回并询问他们。

如果用户正确,则在“回合”重新启动时,它将跳过正确猜测的用户,并继续询问其余用户。

基本上,我要求您查看的范围是在for循环中,然后是SysOut并说"Ok " + name[i] + " Please guess a number.",我希望我的代码可读并且所有建议都应赞赏。谢谢!

while(run == true)
        {
            System.out.println("Welcome to the Random Number Game!");
            System.out.print("How many players are playing?: ");

            while(!sc.hasNextInt())
            {
                System.out.print("Incorrect format, please try again! ");
                sc.next();
            }

            int numPlayers = sc.nextInt(); 

            String[] name = new String[numPlayers];
            int[] randomTarget = new int[numPlayers];
            int[] minNum = new int[numPlayers];
            int[] maxNum = new int[numPlayers];

            System.out.println("What are their names?");

            for(int i = 0; i<numPlayers; i++)
            {
                name[i] = sc.next();            
            }

            for(int i = 0; i<numPlayers; i++)
            {
                System.out.print("Ok, " + name[i] + " Please select a minimum number: ");
                while(!sc.hasNextInt())
                {
                    System.out.print("Incorrect format, please try again! ");
                    sc.next();
                }
                int min = sc.nextInt();
                minNum[i] = min;

                System.out.print("Ok, " + name[i] + " Please select a maximum number: ");
                while(!sc.hasNextInt())
                {
                    System.out.print("Incorrect format, please try again! ");
                    sc.next();
                }
                int max = sc.nextInt();
                maxNum[i] = max;

                randomTarget[i] = (int) ranNum(min,max);
            }

            System.out.println("OK! I've picked a number for you.");

            for(int i = 0; i<numPlayers; i++)
            {
                System.out.print("Ok, " + name[i] + " Please guess the number for me: ");

                while(!sc.hasNextInt())
                {
                    System.out.print("Incorrect format, please try again! ");
                    sc.next();
                }

                int guess = sc.nextInt();
                int x=0;
                int counter = 0;
                int size = maxNum[i] - minNum[i] + 1;
                int[] record = new int[size];

                if(guess != randomTarget[i])
                {       
                    if(x<size)
                    {
                        record[x] = guess;
                        x++;
                    }

                    if(guess>maxNum[i] || guess<minNum[i])
                    {
                        System.out.print("ERROR: Input is not within the bounds you selected. Please try again: "); 

                    }

                    if(guess>randomTarget[i] && guess<=maxNum[i])
                    {
                        System.out.println("Too high, sorry. ");
                        counter++;

                    }
                    if(guess<randomTarget[i] && guess>=minNum[i])
                    {
                        System.out.println("Too low, sorry. ");
                        counter++;
                    }
                    if(guess == randomTarget[i])
                    {
                        System.out.print("Correct!");
                    }

                }
            }

2 个答案:

答案 0 :(得分:0)

您可以执行此操作的一种方法是拥有一个与“名称”数组平行的附加布尔数组。首先创建一个与“名称”数组大小相同的布尔数组:

boolean[] correctGuesses = new boolean[name.size];

如果玩家猜对了,则布尔数组的相同索引将翻转为true。在要求玩家猜测之前,请先检查布尔数组中的相应索引。

或者,考虑一种面向对象的方法。您可以这样创建一个播放器类:

class Player {
    private String name;
    private boolean guessedCorrectly;

    Player(String name) {
        this.name = name;
        guessedCorrectly = false;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean hasGuessedCorrectly() {
        return guessedCorrectly;
    }

    public boolean setGuessedCorrectly(boolean guessedCorrectly) {
        this.guessedCorrectly = guessedCorrectly;
    }
}

答案 1 :(得分:0)

对我来说,最好的方法是使用像failProgrammer所说的类。 通过这种方式,您可以首先向玩家添加其他属性,例如:他们的得分,他们的名字。之后,您只需要使用“ if”球棒来要求每个玩家是否参加比赛。