java - 如果玩家在掷硬币时获胜或失败,则输出错误

标签 java if-statement

如果玩家在我的抛硬币游戏中获胜或失败,我会尝试获得正确的输出。游戏将虚拟硬币翻转 100 次,如果玩家选择了翻转最多的硬币,则获胜,如果少于选择的硬币,则失败。但输出总是错误。

public class Strategy extends Object {

        /**
         * 
         * Encoding for a strategy.
         */

        int opponentLastMove = 1;

        int myLastMove = 1;

        String name;

        // 0 = defect, 1 = cooperate

        public Strategy()

        {

        } /* Strategy */

        public int nextMove()

        {

            return 0;

        } /* nextMove */

        public void saveOpponentMove(int move) {
            opponentLastMove = move;
        }

        public int getOpponentLastMove() {
            return opponentLastMove;
        }

        public void saveMyMove(int move) {
            myLastMove = move;
        }

        public int getMyLastMove() {
            return myLastMove;
        }

        public String getName() {
            return name;
        }

    }

public class StrategyRandom extends Strategy

{

    /**
     * 
     * Encoding for a strategy.
     */

    // 0 = defect, 1 = cooperate

    public StrategyRandom()

    {

        name = "Random";

    } /* StrategyRandom */

    public int nextMove()

    {

        if (Math.random() < 0.5)
            return 1;

        return 0;

    } /* nextMove */

}

import java.util.Scanner;

public class GameDriver {

    public static void main(String[] args) {

        try {

            StrategyRandom stratRandom = new StrategyRandom();

            Scanner scanner = new Scanner(System.in);
            int choice = 1;
            int heads;
            int tails;
            int coinFlip = 0;

            System.out.print("Enter your name: ");
            stratRandom.name = scanner.nextLine();

            System.out.println("Press 1 for Heads" + "\nPress 2 for Tails"
                    + "\nPress 0 to Exit");
            choice = Integer.parseInt(scanner.nextLine());

            while (choice != 0) {

                heads = 0;
                tails = 0;

                for (int i = 0; i < 100; i++) {
                    coinFlip = stratRandom.nextMove();

                    if (coinFlip == 0) {
                        heads++;
                        System.out.print("H,");

                    } else {
                        tails++;
                        System.out.print("T,");

                    }

                }

                if (coinFlip == 1) {

                    System.out.println("\nYou Won");

                } else {

                    System.out.print("\nYou Lost");
                }

                System.out.println("\nTimes head was flipped: " + heads);
                System.out.println("Times tails was flipped: " + tails);
                System.out.print("\nHello " + stratRandom.getName()
                        + ", Enter a number or 0 to exit: ");
                choice = Integer.parseInt(scanner.nextLine());
            }

        } catch (Exception ex) {

            ex.printStackTrace();

        }

    }
}

最佳答案

这行似乎是你的问题。您正在测试最后一次抛硬币是赢还是输,而不是测试谁的总金额更高。

if (coinFlip == 1) {
     System.out.println("\nYou Won");
} else {
     System.out.print("\nYou Lost");
            }

将其更改为

if( choice == 1 ){
    if (heads > tails)
         System.out.println("\nYou Won");
    else if(tails > heads )
         System.out.print("\nYou Lost");
}else{

    if ( heads < tails ) 
        System.out.println("\nYou Won");
    else if( tails < heads )
        System.out.print("\nYou Lost");
}
if( heads == tails )
    System.out.println(\n"It is a tie!");

关于java - 如果玩家在掷硬币时获胜或失败,则输出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33313426/

相关文章:

java - CSVReader 无法正确读取一行

java - 如何在 Intellij IDEA 中的 git 工具栏添加按钮

java - 将一个类的字段复制到另一个类的相同字段

PHP 如果 SteamID 等于 MYSQL 行

mysql 子查询作为 if 条件(仅在需要时)

java - Weblogic 12cR2 Nodemanager native 库无法在 mac os x 上运行

javascript - 使用 jQuery/javascript 验证联系表单中的电子邮件

java - 变量不会更新(在 If 语句内)

javascript - 什么是 javascript if 语句,我可以使用它来仅在选择新选项后显示某些内容?

java - 更改行跨度