java coin折腾模拟器问题

时间:2014-03-12 15:18:42

标签: java computer-science

嗨,我正在为java做一个抛硬币模拟器,必须以某种方式完成。它必须有一个字符串for sideup来保存由no arg构造函数创建的“head”或“tails”字符串,toss方法必须是void并且它必须有一个getsideup方法,然后我们必须运行硬币抛掷20次并且显示头部和尾部的数量...我可以轻松地使用无空方法并且只返回结果,但绕过这个空洞并且获取结果让我疯狂。 这是我到目前为止所做的。

import java.util.Random;
public class coin {
    public static String sideUp;

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

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

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

我希望有人可以告诉我我做错了什么,并把我放在了正确的方向。

5 个答案:

答案 0 :(得分:1)

你在循环开始时没有调用toss()。这需要将值设置为sideUp,并且需要让sideUp更改每次折腾。

答案 1 :(得分:0)

简单的修复在if语句之前放在你的for循环中,但我看到很多可以在这里完成。首先,我将为Coin类添加一个构造函数,并将hcount和tcount添加到类变量中,并创建头尾常量:

private String sideUp;
private int hcount;
private int tcount;
private static final String HEADS = "Heads";
private static final String Tails = "Tails";

Coin()
{
    this.sideUp = HEADS;
    this.hcount = 0;
    this.tcount = 0;
}

然后我会做一个方法来检查折腾:

public  void checkToss()
{
    if (getsideup().equals(HEADS)) 
        hcount++;
    else
        tcount++;
}

现在将toss()和checkCoin()方法添加到if语句之前的for循环中。它应该是这样的:

for (int i = 1; i <= 20; i++)
{
    coin.toss(); 
    coin.checkToss();
}

我也会为头数和尾数计算一个吸气剂:

public int getHeadsCount()
{
    return this.hcount;
}

public int getTailsCount()
{
    return this.tcount;
}

所有放在一起的东西都是这样的:

import java.util.Random;
public class Coin 
{
    private  String sideUp;
    private int hcount;
    private int tcount;
    private static final String HEADS = "Heads";
    private static final String TAILS = "Tails";

    Coin()
    {
        this.sideUp = HEADS;
        this.hcount = 0;
        this.tcount = 0;
    }

    public void toss()
    {
        Random rand = new Random();
        int sideup = rand.nextInt(2);
        if (sideup == 0) 
            sideUp = HEADS;
        else 
            sideUp = TAILS;    
    }

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

    public void checkToss()
    {
    if (getsideup().equals(HEADS)) 
        this.hcount++;
    else
        this.tcount++;
    }

    public int getHeadsCount()
    {
        return this.hcount;
    }

    public int getTailsCount()
    {
        return this.tcount;
    }

    public static void main(String[] args)
    {
        Coin coin = new Coin();
        for (int i = 1; i <= 20; i++) 
        {
            coin.toss();
            coin.checkToss();
        }
        System.out.println("Total Heads = " + coin.getHeadsCount() + " Total Tails = " + coin.getTailsCount());
    }
}

答案 2 :(得分:0)

这个怎么样?

import java.io.*;
import java.math.*;
class Coin_Toss {
public static void main(String args[])throws Exception {
    InputStreamReader ir = new InputStreamReader(System.in);
    BufferedReader br = new BufferedReader(ir);
    double d, drop, side, s_d, re=1;
    int tries=0, score=0;
    while(re==1) {
        tries++;
        System.out.println("-> Hi, please choose your side\n-> Press 1 for    Head\n-> Press 2 for Tails");
        side=Integer.parseInt(br.readLine());
        drop=Math.random();
        d = Math.round(drop);
        if(d== 0) {
            s_d = 1;
        }
        else {
          s_d = 2;
        }
        if(s_d == side) {
            System.out.println("-> You Won");
            score++;
        }
        else {
            System.out.println("-> You Lose");
        }
        if(s_d==1) {
           System.out.println("->Its Heads");
        }
        else if(s_d==2) {
           System.out.println("-> Its tails");
        }
        System.out.println("-> Press 1 to Toss again \n -> Press 2 to     Quit");
        re=Double.parseDouble(br.readLine());
        if(re==2) {
            System.out.println("You Scored "+score+" out of "+tries);
        }
    }
}
}

答案 3 :(得分:-1)

for(int i = 1; i <= 20; i++) 
    {
        toss();
      if (getsideup().equals("heads")) 
      {
        hcount++;
      } else 
      {
        tcount++;
      }
    }

答案 4 :(得分:-1)

import java.util.Random;
public class coin
{
public static String sideUp;
public int hcount=0;
public int tcount=0;

public static void toss()
     {
        Random rand = new Random();
        int sideup = rand.nextInt(2);
        if (sideup == 0)
            {
                sideUp = "heads";
                hcount++;
            }
            else 
            {
                sideUp  = "tails";
                tcount++;
            } 
      }       

  public static void main(String[] args)
  {
    for(int i=0;i<20;i++)
    {toss();}
    System.out.println("total heads = " + hcount + " total tails = " + tcount);
   }
}