【问题标题】:Creating complex probabilities within Java在 Java 中创建复杂的概率
【发布时间】:2012-10-28 11:51:39
【问题描述】:

我正在尝试在 java 中创建棒球模拟游戏。我正在使用“间距”的实例作为我系统的迭代。在此范围内,我对结果有几种不同的可能性。击中,未命中(罢工),界外球(无效)。我从另一个类中创建了一组播放器,这些播放器读取了我设计的播放器的特定属性。我目前试图利用的唯一属性是击球手的力量和他们的一致性。我正在使用随机数来生成某个值,并根据该值所在的位置确定它是球还是罢工。 “球”逻辑简单且有效;但是,我只收到击球手的球数。我被困在如何实现关于击球的击球(错过挥杆)或击球概率的逻辑。我为播放器使用的构造函数如下

Player a = new Player(false, false, true, 10, 20, 0.75, true, null); 

你可以忽略false's和true's和null,只注意数字 第一个数字(10)表示速度,不相关。 第二个数字(20)表示功率。 第三个表示击球手的一致性。

如果这可能会造成混淆或过于简单,我深表歉意,我只编程了一个多月。所有帮助将不胜感激。目前对我来说唯一的打印看起来有点像

Press 'p' to initiate pitches
p
Ball count is: (0,0)
Ball count is: (0,0)
Ball count is: (0,0)
Ball count is: (0,0)
Ball! 
Ball count is: (1,0)
Ball count is: (1,0)
Ball! 
Ball count is: (2,0)
Ball count is: (2,0)
Ball count is: (2,0)
Ball! 
Ball count is: (3,0) 

我不明白为什么程序只识别球而不是把打印的内容描述为空,我认为它是界外球(但它没有打印我的声明)

请帮忙,非常感谢!

import java.util.Scanner;
public class Game {

public static void main(String[] args) {
System.out.println("Press 'p' to initiate pitches");
Scanner kb = new Scanner(System.in);
String s = kb.nextLine();
if(s.equalsIgnoreCase("p"))
{
    int ball = 0;
        int strike = 0;
    //10 instances of pitches
    for(int i = 0; i < 10; i++)
    {
    double number = Math.random();
    if(number > 0.5)
    {
        ball++;
        if(ball == 4)
        {
        System.out.println("Ball four, take your base");
        break;
        }
        System.out.print("Ball!");
    }
    else if(strike() == true)
    {
       {
            if(isMiss() == true)
        {
            System.out.print(" Strike!");
            strike++;
        }
        else if(isFoul() == true)
        {
            System.out.println("Foul ball!");
        }
        }
        if(strike == 3)
        {
        System.out.print(" Player struck out!");
        break;
        }
            }
    else
    {
        if(isHit() == true)
        {
        System.out.println("The ball was hit!");
        System.out.println(isHit());
        break;
        }
    }
    System.out.println(" Ball count is: " + "(" + ball + "," + strike + ")");
    }
}
}

public static boolean strike()
{
if(isMiss() == true)
{
        return true;
}
else if(isFoul() == true)
{
        return false;
}
else if(isHit() == true)
{
        return true;
}
return false;
}

public static boolean isHit()
{
double probability = Math.random();
Player a = new Player(false, false, true, 10, 20, 0.75, true, null);
if(a.power > 5)
{
    if(a.consistency > 0.5)
    {
            if(probability > 3 && probability < 6)
            {
                return false;
            }
            if(probability > 6 && probability < 9)
            {
                return false;
            }
            if(probability > 9 && probability < 12)
            {
                return true;
            }
            return true;
        }
        System.out.println("The ball was hit!");
    }
    return false;
}

public static boolean isMiss()
{
    double probability = Math.random();
    Player a = new Player(false, false, true, 10, 20, 0.75, true, null);
    if(a.power > 5)
    {
        if(a.consistency > 0.5)
        {
            if(probability > 3 && probability < 6)
            {
                return true;
            }
            if(probability > 6 && probability < 9)
            {
                return false;
            }
            if(probability > 9 && probability < 12)
            {
                return false;
            }
            return false;
        }
    }
    return false;
}

public static boolean isFoul()
{
    double probability = Math.random();
    Player a = new Player(false, false, true, 10, 20, 0.75, true, null);
    if(a.power > 5)
    {
        if(a.consistency > 0.5)
        {
            if(probability > 3 && probability < 6)
            {
                return false;
            }
            if(probability > 6 && probability < 9)
            {
                return true;
            }
            if(probability > 9 && probability < 12)
            {
                return false;
            }
        }

    }
    return false;
}

【问题讨论】:

  • probability &gt; 6 return true;. 应该更改这个变量名。
  • 您的括号({})彼此不匹配。例如 else if(strike() == true) 之后的 2x {

标签: java probability


【解决方案1】:

我建议您使用调试器单步执行您的代码,因为有很多部分对我来说没有意义,并且我无法使用 Player 类为您运行您的代码(而且我无法弥补它,因为它没有'没有意义;)

没有意义的代码部分是

double probability = Math.random();

所以概率是介于 [0, 1) 之间的数字

if(probability > 3 && probability < 6) // always false.
if(probability > 6 && probability < 9) // always false
if(probability > 9 && probability < 12) // always false.

// prints the ball was hit but returns `false` to isHit
System.out.println("The ball was hit!");
}
return false;

【讨论】:

    【解决方案2】:

    对于罢工:

    if (Math.random() > consistency) { /* strike! */ }
    

    你可能想要一个恒定的犯规机会(比如如果他们击球,他们有 10% 的机会犯规:

    else if (Math.random() < 0.1) { ... }
    

    功率也一样,但可能乘以 0.3(因为本垒打很少见):

    if (Math.random() < power * 0.3) { ... }
    

    请注意,要使这些功能起作用,您的一致性和功率变量必须是小数。

    例如,50% 的一致性为 0.5。 20% 的一致性为 0.2。同样,1 是最大功率,0 是非常弱的。 0.5 介于两者之间。

    【讨论】:

    • 那么,我是否像 public static final double FOUL_BALL = 0.1; 这样实现常量? ?
    • 是的,你可以这样做。您还应该将变量一致性和功率更改为双精度数。
    • 我应该在 if 语句的逻辑中实现 math.random() > 一致性?
    • 好的,接下来的两个 if 语句是关于什么的?我会在罢工中实施它们吗?
    • 阅读我写的内容,这将是不言自明的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-14
    • 2012-01-01
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-26
    相关资源
    最近更新 更多