在Java中创建复杂概率

在Java中创建复杂概率,java,probability,Java,Probability,我试图在java中创建一个棒球模拟游戏。我正在利用“Pitchs”实例作为系统迭代的基础。在这一点上,我对结果有几种不同的可能性。击中、未击中(一击)、界外球(无效)。我从另一个类中创建了一个玩家数组,读取我设计的玩家的特定属性。我目前试图利用的唯一属性是打击者的力量和他们的一致性。我使用随机数来生成一个特定的值,并根据该值所在的位置来确定它是一个球还是一个一击。“球”逻辑简单有效;然而,我只收到击球手的球数。我被困在如何实现一次击球(一次失误的挥棒)或一次击球的概率逻辑上。我为播放器使用的构造

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

Player a = new Player(false, false, true, 10, 20, 0.75, true, null); 
您可以忽略false和true以及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;
}
import java.util.Scanner;
公开课游戏{
公共静态void main(字符串[]args){
System.out.println(“按“p”开始投球”);
扫描仪kb=新扫描仪(System.in);
字符串s=kb.nextLine();
如果(s.equalsIgnoreCase(“p”))
{
int-ball=0;
int-strike=0;
//10次投球
对于(int i=0;i<10;i++)
{
double number=Math.random();
如果(数字>0.5)
{
ball++;
如果(球==4)
{
System.out.println(“球四,打你的垒”);
打破
}
系统输出打印(“球!”);
}
else if(strike()==true)
{
{
如果(isMiss()==true)
{
系统输出打印(“罢工!”);
strike++;
}
else if(isfould()==true)
{
System.out.println(“界外球!”);
}
}
如果(罢工==3)
{
System.out.print(“玩家出局!”);
打破
}
}
其他的
{
如果(isHit()==true)
{
System.out.println(“球被击中了!”);
System.out.println(isHit());
打破
}
}
System.out.println(“球数为:“+”(“+Ball+”,“+strike+”));
}
}
}
公共静态布尔罢工()
{
如果(isMiss()==true)
{
返回true;
}
else if(isfould()==true)
{
返回false;
}
else if(isHit()==true)
{
返回true;
}
返回false;
}
公共静态布尔值isHit()
{
双重概率=数学随机();
玩家a=新玩家(假、假、真、10、20、0.75、真、空);
如果(a.功率>5)
{
如果(a.一致性>0.5)
{
如果(概率>3&&概率<6)
{
返回false;
}
如果(概率>6&&概率<9)
{
返回false;
}
如果(概率>9&&概率<12)
{
返回true;
}
返回true;
}
System.out.println(“球被击中了!”);
}
返回false;
}
公共静态布尔isMiss()
{
双重概率=数学随机();
玩家a=新玩家(假、假、真、10、20、0.75、真、空);
如果(a.功率>5)
{
如果(a.一致性>0.5)
{
如果(概率>3&&概率<6)
{
返回true;
}
如果(概率>6&&概率<9)
{
返回false;
}
如果(概率>9&&概率<12)
{
返回false;
}
返回false;
}
}
返回false;
}
公共静态布尔值isfould()
{
双重概率=数学随机();
玩家a=新玩家(假、假、真、10、20、0.75、真、空);
如果(a.功率>5)
{
如果(a.一致性>0.5)
{
如果(概率>3&&概率<6)
{
返回false;
}
如果(概率>6&&概率<9)
{
返回true;
}
如果(概率>9&&概率<12)
{
返回false;
}
}
}
返回false;
}
对于罢工:

if (Math.random() > consistency) { /* strike! */ }
你可能想要一个固定的犯规机会(比如如果他们击中了球,他们有10%的犯规机会:

else if (Math.random() < 0.1) { ... }
else如果(Math.random()<0.1){…}
功率也一样,但可能乘以0.3(因为全垒打很少):

if(Math.random()
请注意,要使这些变量起作用,一致性和幂变量必须是小数

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

我建议您使用调试器逐步检查代码,因为有许多部分对我来说没有意义,并且我无法为您运行与Player类一起使用的代码(我无法编写,因为它没有意义;)

代码中没有意义的部分是

double probability = Math.random();
所以概率是一个介于[0,1]之间的数字

if(概率>3&&probability<6)//始终为false。
if(概率>6&&概率<9)//始终为false
if(概率>9&&概率<12)//始终为false。
//打印球
double probability = Math.random();
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;