java - 在 Java 中创建复杂的概率

标签 java probability

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

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

最佳答案

我建议你使用你的调试器来单步执行你的代码,因为有很多部分对我来说没有意义,我无法为你运行你的代码,使用 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;

关于java - 在 Java 中创建复杂的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13286410/

相关文章:

java - 在 SQL 语句中调用具有默认值的函数

python - Keras 分类器 predict_proba() 与 predict() 不匹配

c++ - 使用 Boost::accumulators 查找超过 (µ + 3 sigma) 的值

java表达式从图像数据中删除dat/image部分

php - 固定比例选择

php - MySQL SELECT 概率喜恶系统

Python - NLTK 中的三元组概率分布平滑技术 (Kneser Ney) 返回零

java - Hibernate,从抽象父对象获取子对象

java - 使用参数数组实例化对象列表

java - 如何正确编写 JUnit 测试?