首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何制作一个5人猜游戏(基本)

如何制作一个5人猜游戏(基本)
EN

Stack Overflow用户
提问于 2015-09-15 07:57:40
回答 2查看 84关注 0票数 0

我在这段代码中遇到了错误。这是我们的案例研究,所以我们需要创建这个程序仅使用javax.swing,同时,如果其他。

==my代码似乎只工作到播放器3。当我键入Player 4和5时,它只是terminates.==

有什么不对吗?因为eclipse没有说什么。我的教授说要把它变成“循环中的循环”而不是复制粘贴,但我很难弄清楚.

我只需要修正第4和第5位玩家的错误.

提前谢谢

代码语言:javascript
复制
import javax.swing.*;

public class CaseStudyHW {

    public static void main(String[] args) {
        int guess;
        int magic = (int) (Math.random() * 10 + 1);
        int attempt1 = 1;
        int attempt2 = 1;
        int attempt3 = 1;
        int attempt4 = 1;
        int attempt5 = 1;
        int players;

        players = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Players. \n2-5 Only"));
        if (players == 2) {
            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 1, Enter A Number from 1-10", "Guessing Game", 1));
                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt1++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0);
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);

            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt2++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);
            if (attempt1 > attempt2) {
                JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
            } else {
                JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
            }
        } else if (players == 3) {
            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, "Player 1, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt1++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);

            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null,                        "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt2++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);

            do {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                        "Player 3, Enter A Number from 1-10", "Guessing Game", 1));

                if (guess == magic) {
                    JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                            + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)", "Guessing Game", 1);
                } else {
                    attempt3++;
                    if (guess > magic) {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                        );
                    } else {
                        JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                    }
                }
            } while (guess != magic);
            if (attempt1 > attempt2) {
                JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
            } else if (attempt2 > attempt3) {
                JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
            } else if (attempt3 > attempt1) {
                JOptionPane.showMessageDialog(null, "Player 3 Won!", "Guessing Game", 0);
            } else if (players == 4) {
                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 1, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt1++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);

                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt2++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);

                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 3, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt3++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);

                do {
                    guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                            "Player 4, Enter A Number from 1-10", "Guessing Game", 1));

                    if (guess == magic) {
                        JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                    } else {
                        attempt4++;
                        if (guess > magic) {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                            );
                        } else {
                            JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                        }
                    }
                } while (guess != magic);
                if (attempt1 > attempt2) {
                    JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
                } else if (attempt2 > attempt3) {
                    JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
                } else if (attempt3 > attempt1) {
                    JOptionPane.showMessageDialog(null, "Player 3 Won!", "Guessing Game", 0);
                } else if (attempt4 > attempt3) {
                    JOptionPane.showMessageDialog(null, "Player 4 Won!", "Guessing Game", 0);
                } else if (players == 5) {
                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 1, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt1++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 2, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt2++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 3, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt3++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 4, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt4++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);

                    do {
                        guess = Integer.parseInt(JOptionPane.showInputDialog(null,
                                "Player 5, Enter A Number from 1-10", "Guessing Game", 1));

                        if (guess == magic) {
                            JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!"
                                    + "\nThe Number is " + magic + "\nYou Made a Total of " + attempt5 + " attempt(s)", "Guessing Game", 1);
                        } else {
                            attempt4++;
                            if (guess > magic) {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!", "Guessing Game", 0
                                );
                            } else {
                                JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!", "Guessing Game", 0);
                            }
                        }
                    } while (guess != magic);
                    if (attempt1 > attempt2) {
                        JOptionPane.showMessageDialog(null, "Player 1 Won!", "Guessing Game", 0);
                    } else if (attempt2 > attempt3) {
                        JOptionPane.showMessageDialog(null, "Player 2 Won!", "Guessing Game", 0);
                    } else if (attempt3 > attempt1) {
                        JOptionPane.showMessageDialog(null, "Player 3 Won!", "Guessing Game", 0);
                    } else if (attempt4 > attempt3) {
                        JOptionPane.showMessageDialog(null, "Player 4 Won!", "Guessing Game", 0);
                    } else if (attempt5 > attempt4) {
                        JOptionPane.showMessageDialog(null, "Player 4 Won!", "Guessing Game", 0);
                    } else {
                        JOptionPane.showMessageDialog(null, "No Such Gamemode", "Guessing Game", 0);
                    }
                }
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-09-15 09:38:05

现在你错过了一个}

代码语言:javascript
复制
else if (attempt3 > attempt1) {
    JOptionPane.showMessageDialog(null, "Player 3 Won!", "Guessing Game", 0);
}

在此之后

代码语言:javascript
复制
else if (attempt4 > attempt3) {
    JOptionPane.showMessageDialog(null, "Player 4 Won!", "Guessing Game", 0);
}

编辑:删除方法。与其拥有大量复制粘贴代码,您还可以为每个播放器使用一个循环:

代码语言:javascript
复制
public static void main(String[] args) {
    int guess;
    int magic = (int) (Math.random() * 10 + 1);
    // Note that attemps are now initialized as 0
    int attempt1 = 0;
    int attempt2 = 0;
    int attempt3 = 0;
    int attempt4 = 0;
    int attempt5 = 0;
    // Or an int-array for attempts
    int players;

    // Get the amount of players
    do {
        players = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Players. \n2-5 Only"));
        // Notify user if players < 2 || > 5
    } while (players < 2 || players > 5);

    // Play game for each player
    int player = 0;
    do {
        // Logic for a single player here, namely:
        // Increase the attempt based on the player, if you
        // know what an int-array is, use this. Always increase the
        // attempt, because a succesfull attempt is still an attempt.
        // Request input
        // Check if the guess equals the magic number
        // Notify the user
        player++;
    } while (player < players);

    // Notify winner based on lowest attempt, but only if attempt > 0.
    // This is why I've initialized attempts as 0.
}
票数 0
EN

Stack Overflow用户

发布于 2015-09-25 02:10:58

好的,所以我们的proffesor移走了5名球员,改为了3名。如果“如果”( player =‘3’)使程序忽略用户输入,那么当我删除播放机号码上的“”时,它就会正常工作。下面是3播放器程序的完整功能代码:

代码语言:javascript
复制
public static void main(String[] args) {
            int guess;
            int magic = (int) (Math.random() * 10 + 1);
            int attempt1 = 1;
            int attempt2 = 1;
            int attempt3 = 1;
            int players;

            players = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter Number of Players. \n2-3 Only"));
            if(players == 2)
            {
                do
                {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, 
                       "Player 1, Enter A Number from 1-10","Guessing Game",1));

                if(guess == magic)
                   JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" +
                           "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)","Guessing Game",1);
                else
                {
                attempt1++;
                if(guess < magic)
                   JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!"
                           ,"Guessing Game",0
                           );
                else
                   JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!"
                           ,"Guessing Game",0);
                }
                }while(guess != magic);

            do
            {
            guess = Integer.parseInt(JOptionPane.showInputDialog(null, 
                   "Player 2, Enter A Number from 1-10","Guessing Game",1));

            if(guess == magic)
               JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" +
                       "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)","Guessing Game",1);
            else
            {
            attempt2++;
            if(guess < magic)
               JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!"
                       ,"Guessing Game",0
                       );
            else
               JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!"
                       ,"Guessing Game",0);
            }
            }while(guess != magic);
            if(attempt1<attempt2)
                JOptionPane.showMessageDialog(null, "Player 1 Won with " + attempt1 + "attempts"
                       ,"Guessing Game",0);
            else
                JOptionPane.showMessageDialog(null, "Player 2 Won  Won with " + attempt2 + "attempts"
                       ,"Guessing Game",0);
            }


    else if(players == 3)
            {
                do
                {
                guess = Integer.parseInt(JOptionPane.showInputDialog(null, 
                       "Player 1, Enter A Number from 1-10","Guessing Game",1));

                if(guess == magic)
                   JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" +
                           "\nThe Number is " + magic + "\nYou Made a Total of " + attempt1 + " attempt(s)","Guessing Game",1);
                else
                {
                attempt1++;
                if(guess < magic)
                   JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!"
                           ,"Guessing Game",0
                           );
                else
                   JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!"
                           ,"Guessing Game",0);
                }
                }while(guess != magic);

            do
            {
            guess = Integer.parseInt(JOptionPane.showInputDialog(null, 
                   "Player 2, Enter A Number from 1-10","Guessing Game",1));

            if(guess == magic)
               JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" +
                       "\nThe Number is " + magic + "\nYou Made a Total of " + attempt2 + " attempt(s)","Guessing Game",1);
            else
            {
            attempt2++;
            if(guess < magic)
               JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!"
                       ,"Guessing Game",0
                       );
            else
               JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!"
                       ,"Guessing Game",0);
            }
            }while(guess != magic);

            do
            {
            guess = Integer.parseInt(JOptionPane.showInputDialog(null, 
                   "Player 3, Enter A Number from 1-10","Guessing Game",1));

            if(guess == magic)
               JOptionPane.showMessageDialog(null, "Correct! You Guessed the Magic Number!" +
                       "\nThe Number is " + magic + "\nYou Made a Total of " + attempt3 + " attempt(s)","Guessing Game",1);
            else
            {
            attempt3++;
            if(guess < magic)
               JOptionPane.showMessageDialog(null, "Sorry! The number you gave is higher than the Hidden Number! Try Again!"
                       ,"Guessing Game",0
                       );
            else
               JOptionPane.showMessageDialog(null, "Sorry! The number you gave is lower than the Hidden Number! Try Again!"
                       ,"Guessing Game",0);
            }
            }while(guess != magic);

            if(attempt1 < attempt2)
                if(attempt1 < attempt3)
                JOptionPane.showMessageDialog(null, "Player 1 Won with " + attempt1 + "attempts"
                       ,"Guessing Game",0);
            else if(attempt2 < attempt3)
                if(attempt2 < attempt1)
                JOptionPane.showMessageDialog(null, "Player 2 Won with " + attempt2 + "attempts"
                       ,"Guessing Game",0);
            else if(attempt3 < attempt2)
                if(attempt3 < attempt1)
                JOptionPane.showMessageDialog(null, "Player 3 Won with " + attempt3 + "attempts"
                       ,"Guessing Game",0);
            }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32580684

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档