我正嘗試在java中創建一個棒球模擬遊戲。我正在利用「pitch」的實例作爲我係統的迭代。在此範圍內,我對結果有幾種不同的可能性。打,小姐(打),犯規球(沒有效果)。我從另一個班級創建了一系列玩家,這些玩家閱讀了我設計的玩家的特定屬性。我正在嘗試利用的唯一屬性是擊球手的力量和他們的一致性。我使用隨機數來產生一定的數值,並取決於數值的位置,決定它是一個球還是一個擊球。 '球'邏輯很簡單,有效運作;然而,我只接受擊球手的球數。我被困在如何實現罷工概率(錯過的揮杆)或關於球場是罷工的命中的邏輯。我使用的播放器的構造函數去如下在Java中創建複雜概率
Player a = new Player(false, false, true, 10, 20, 0.75, 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;
}
'概率> 6返回之間的數真的;'。應該是可能的你改變這個變量名稱。 – UmNyobe
你的括號('{'和'}')不匹配。例如2x'{''在'else之後if(strike()== true)' –