我正尝试在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)' –