【问题标题】:Java Coin Flip ProgramJava 硬币翻转程序
【发布时间】:2016-09-15 21:35:33
【问题描述】:

我正在尝试编写一个简单的硬币翻转程序,并且想知道是否可以得到一些帮助。我对 Java 还很陌生,只是想问用户他们想抛硬币多少次。这是我的代码:

package cointossing;
import java.util.Random;
import java.util.Scanner;
import static java.lang.System.in;
import static java.lang.System.out;
/**
 *  Coin tossing class to simulate the flip of a coin 
 *  with two sides; Heads and Tails.
 * 
 * @author Alex Chapman ID:
 *  
 */
public class CoinTossing 
{    
    public static String sideUp;
    public static int number;

      public void run()
    {
        try( Scanner input = new Scanner(in) ) 
        {
            out.print("Enter how many times you would like to flip the coin");
            out.print("if you enter 0 the program quits");
            int number = input.nextInt();    
        }
    }


    private static void coin() 
    {
        Random rand = new Random();
        int sideup = rand.nextInt(2);
        if (sideup == 0) 
        {
            sideUp = "heads";
        } 
        else 
        {
            sideUp = "tails";
        }
    }

    public static String getsideup() 
    {
        out.println(sideUp);
        return sideUp;
    }



    public static void main(String[] args) 
    {
        int hcount = 0;
        int tcount = 0;
        for (int i = 1; i <= number; i++) 
        {
            coin();
            if (getsideup().equals("heads")) 
            {
                hcount++;
            } 
            else 
            {
                tcount++;
            }
        }
        out.println("total heads = " + hcount + " total tails = " + tcount);
    }
}

但是当我运行该程序时,它会跳过询问用户任何内容并仅显示 0,因为没有次数可以翻转硬币...我觉得我在正确的轨道上但我卡住了...任何帮助将不胜感激..

编辑:

因此,为了学习,我决定更改我的程序并将硬币更改为枚举并让程序返回该枚举值。我还将用户输入更改为菜单样式,但这有助于遵循在我不久前购买的一本 Barnes and Nobles 书中……我想我已经走到了一个奇怪的十字路口……我想基本上将这两个程序融合在一起,以便所有新工作都能返回枚举值这样仍然存在,但删除了“菜单”方面并用用户输入他们想从以前的程序中进行多少次翻转的能力取而代之。这是我写的新程序:

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);
    }
}

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

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

然后减少计数,我将不得不检查该计数,一旦达到 0 打印结果,最多有多少个正面和多少个反面,然后提示用户他们是否想再次玩游戏..老实说我什至不知道我为什么要这样做,如果不是为了知识方面,所以如果你不想帮助一个 broski,我理解。

【问题讨论】:

  • 你可以发布完整的代码而不是两个断开的段?
  • 尝试在int number = input.nextInt(); 行之后添加input.nextLine();
  • 如果在显示说明的地方使用println 而不是print 会发生什么?
  • input.nextLine();什么都没做..
  • println 也没有

标签: java


【解决方案1】:

您没有在 main 中调用 run()。

您需要在调用for (int i = 1; i &lt;= number; i++) 之前添加run()

您还需要再次检查您的变量,看起来您正在使用 sideUp 作为一个 int 和一个字符串。当您设置值 "heads""tails" 时,尝试在您的 coin() 调用中添加 this.sideUp,或重命名您的 int sideUp 变量以避免混淆。

【讨论】:

    【解决方案2】:

    改变你的主要方法,像这样:

    public static void main(String[] args) 
        {
            int hcount = 0;
            int tcount = 0;
            Scanner sc = new Scanner(System.in);
            out.println("How many coin flips do you want?");
            int number = sc.nextInt();
            for (int i = 1; i <= number; i++) 
            {
                coin();
                if (getsideup().equals("heads")) 
                {
                    hcount++;
                } 
                else 
                {
                    tcount++;
                }
            }
            System.out.println("total heads = " + hcount + " total tails = " + tcount);
        }
    

    【讨论】:

    • 很好,如果您喜欢,请阅读meta.stackexchange.com/questions/5234/…,谢谢!
    • 对Blankets 做错了什么的一些解释会比一大堆代码更好。这样一来,Blankets 就可以真正学到一些东西。
    • 这是他的代码,只是移动了 3 行,他知道现在发生了什么。
    • 他从未在他的代码中调用 run(),而您通过添加 3 行而不是 1 行来模拟。
    • 是的,我这样做了,因为调用方法run 不是一个好主意(线程方法)。
    【解决方案3】:

    很短的一个

    package BuildProjects;
    import java.util.Random;
    public class HeadsTails {
    
        public static void main(String[] args) {
            
            int coin ;
            
            // Random class
            
            Random rand = new Random();
            
            coin = rand.nextInt(4);
            
            if ( coin == 1) {
                System.out.println("Heads");
            }
            
            else 
                System.out.println("Tails");
        }    
    }
    

    【讨论】:

    • 这个答案不正确。 OP 想要一个输入提示,而这段代码没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-30
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多