如何制作5人猜测游戏(基础)

时间:2015-09-15 07:57:41

标签: java swing

我在此代码中遇到错误。这是我们的案例研究,所以我们只需要使用javax.swing,do-while和if else来创建这个程序。

==我的代码似乎只适用于播放器3.当我输入播放器4和5时,它只是终止。==

任何错误?因为日食并不是说东西。我的教授说了一些关于让它成为“循环中的循环”的东西,而不是复制粘贴,如果不是,但是我很难搞清楚......

我只需要解决第4和第5个玩家错误....

先谢谢

if($('body').hasClass('post-type-my_post_type')) {
  // do stuff
}

2 个答案:

答案 0 :(得分: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);
}

编辑:删除了方法。您可以为每个玩家使用循环,而不是拥有大量的复制粘贴代码:

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.
}

答案 1 :(得分:0)

好的,所以我们的proffesor删除了5个玩家并取而代之的是3个玩家。如果(玩家=='3'),则''使得程序忽略用户输入,当我删除玩家编号上的''时,它有效。这是3人游戏程序的全功能代码:

next_result()

}