【发布时间】: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 > 6 return true;. 应该更改这个变量名。 -
您的括号(
{和})彼此不匹配。例如else if(strike() == true)之后的 2x{
标签: java probability