替换'菜单'来自用户的输入

时间:2016-09-16 22:02:00

标签: java

为了学习,我决定写一个硬币翻转程序。硬币是一个枚举,我有程序返回枚举值。我也从菜单风格中获得了用户输入,但这是通过跟随我在一段时间内购买的Barnes and Nobles书中得到的帮助。

我想我已经走到了一条奇怪的十字路口。我想基本上返回枚举值,但删除菜单'方面并将其替换为用户输入他们想要做多少翻转的能力,并且如果他们想要也重复该程序(因此,每次他们可以输入200而不是按1翻转它会翻转那么多我也认为这样做会有助于公平检查,因为公平的真正测试会返回几乎均匀的头部和尾部,如果要翻转那么多次然后按0表示没有翻转会结束程序)我希望提示用户并询问他们是否愿意重复。

这是我写的程序:

import java.util.*;

public class CoinTossing
{
    private enum Coin { HEADS, TAILS };

    private static final Random randomNumbers = new Random();

    private static final int HEAD = 1;
    private static final int TAIL = 2;

    public static void main(String[] args)
    {
        Scanner input = new Scanner( System.in );

        int choice;
        int toss = 0;
        int tosses = 0;
        int frontflip = 0;
        int backflip = 0;

        Coin gameStatus;

        System.out.println("Welcome to the Coin Toss Program.\n");
        System.out.println("Choose from the menu what you want to do.");
        System.out.print("1. Toss the coin\n2. Quit program\n");
        choice = input.nextInt();

        while ( choice != 0 )
        {
            if ( choice == 1 )
            {
                int CoinTossed = Flip();

                switch ( CoinTossed )
                {
                                //added tosses to switch statement to make the counter work perfect.
                case HEAD:
                    gameStatus = Coin.HEADS;
                    tosses++; // add amount of tosses
                    break;
                default: // changed case TAIL to default. Easy and works.
                    gameStatus = Coin.TAILS;
                    tosses++; // add amount of tosses
                    break;
                }

                if ( gameStatus == Coin.HEADS )
                {
                    frontflip++; //Add amount of heads
                }
                else // gameStatus == TAILS
                    backflip++; //Add amount of tails       
            }

            // A try to make an real exit out of a program

            if ( choice == 2 )
            {
                EndProgram( frontflip, backflip, tosses );
            }

            System.out.println("\nChoose from the menu what you want to do.");
            System.out.print("1. Toss the coin\n2. Quit program\n");
            choice = input.nextInt();   
        }   
    }

    //Toss the coin to determine 1 or 2.
    public static int Flip()
    {
        int toss;

        toss = 1 + randomNumbers.nextInt( 2 );

        if ( toss == 1 )
        {
            System.out.println("You toss the coin and it lands on head!");
        }
        else
        {
            System.out.println("You toss the coin and it lands on tail!");
        }
        return toss;
    }

    public static void EndProgram( int frontflip, int backflip, int tosses )
    {
        System.out.printf("You have tossed %d times.\n", tosses);
        System.out.printf("Of all those tosses, %d landed on heads, and %d on tails.\n", frontflip, backflip);
        System.exit(0);
    }
}

我想我需要一个do / while循环,以便我可以让用户回答你是否想再次播放的是或否的问题?在循环内部我有一个switch语句,如果用户为程序结束的翻转次数输入0,还会说明这一点?

我以为我可以添加这个片段来获取输入:

System.out.println("How many flips do you want?");
System.out.println("(0 will exit the program)");
number = input.nextInt();

我正在考虑创建一个新变量并让用户设置投掷次数。然后像这样复合while循环检查

while(choice != 0 && numTosses !=0)

然后减少计数并且我必须检查该计数并且一旦达到0打印结果,只要有多少头和多少尾巴然后提示用户他们是否想再次玩游戏但是我无法正确行事。说实话,我甚至都不知道为什么我会尝试这样做,但是对于知识方面,所以如果你不想帮助我知道的broski。我觉得自己走在正确的轨道上。

1 个答案:

答案 0 :(得分:0)

您可以使用2个循环:

public class CoinFlip {

    private enum Coin {
        HEADS,
        TAILS
    };

    public static void main(String[] arguments) {

        new CoinFlip();
    }

    CoinFlip() {

        Scanner input = new Scanner(System.in);
        int heads = 0, tails = 0;

        while (true) {
            System.out.println("How many flips do you want?");
            System.out.println("(0 will exit the program)");
            int number = input.nextInt();
            if (number == 0)
                break; // or System.exit

            for (int i = 0; i < number; i++) {
                Coin flipResult = flip();
                switch (flipResult) {
                    case HEADS:
                        heads++;
                        break;
                    case TAILS:
                        tails++;
                        break;
                }
            }
            System.out.println("Heads: " + heads);
            System.out.println("Tails: " + tails);
        }
    }

    private Coin flip() {

        return Coin.values()[(int) (Math.random() * Coin.values().length)];
    }
}

while循环继续要求用户一次又一次地播放,如果他们将0打破或退出它。在该循环中,for循环将遍历翻转。