我正在制作一个可以创造10个足球运动的程序。我有三个随机对象。一个人随机播放并播放。第二个拉随机数获得正增益,第三个随机抽取随机数,当四分卫被解雇时。
我遇到的问题是我希望能够在循环结束后计算出驱动器的总码数(正码 - 负码)并且我无法正确计算出数字。有任何想法吗?
public void callPlays(){
Random random = new Random(); //plays and players
Random yardsGained = new Random(); //positive plays
Random yardsLost = new Random(); //negative plays
int totalYards=0;
int numberOfYards = 0;
int lossOfYards = 0;
System.out.println("Redskins begin drive on their own 20 yard line");
for(int i=0;i<10;i++)
{
numberOfYards = yardsGained.nextInt(16);
lossOfYards = yardsLost.nextInt(16);
String[] passCatchers = new String[6];
passCatchers[0] = "DeSean Jackson";
passCatchers[1] = "Pierre Garcon";
passCatchers[2] = "Jamison Crowder";
passCatchers[3] = "Jordan Reed";
passCatchers[4] = "Matt Jones";
passCatchers[5] = "Chris Thompson";
int passCatcher = random.nextInt(passCatchers.length);
String[] backs = {"Matt Jones", "Chris Thompson"};
int runningBack = random.nextInt(backs.length);
String[] plays = new String[7];
plays[0] = "Pass complete to " + passCatchers[passCatcher]
+ " for " + numberOfYards + " yards.";
plays[1] = "Handoff to " + backs[runningBack] + " along the left
edge for " + numberOfYards + " yards.";
plays[2] = "Handoff to " + backs[runningBack] + " up the middle for
" + numberOfYards + " yards.";
plays[3] = "Handoff to " + backs[runningBack] + " along the right
edge for " + numberOfYards + " yards.";
plays[4] = "Screen pass to " + passCatchers[passCatcher] + " for "
+ numberOfYards + " yards";
plays[5] = "Kirk Cousins scrambles for " + numberOfYards + "
yards";
plays[6] = "Kirk Cousins sacked for " + lossOfYards + " yards.";
int select = random.nextInt(plays.length);
System.out.println(plays[select]);
}
totalYards = numberOfYards - lossOfYards; //does not add up correctly
System.out.println("10 play drive. " + totalYards + " yards.")
答案 0 :(得分:0)
在您的情况下,您似乎只获得了上次迭代的yardsGained和yardsLost值。您需要在每次迭代中添加/减少这些。试试以下。
public void callPlays() {
Random random = new Random(); //plays and players
Random yardsGained = new Random(); //positive plays
Random yardsLost = new Random(); //negative plays
int totalYards=0;
int numberOfYards = 0;
int lossOfYards = 0;
System.out.println("Redskins begin drive on their own 20 yard line");
for(int i=0;i<10;i++)
{
numberOfYards = yardsGained.nextInt(16);
lossOfYards = yardsLost.nextInt(16);
String[] passCatchers = new String[6];
passCatchers[0] = "DeSean Jackson";
passCatchers[1] = "Pierre Garcon";
passCatchers[2] = "Jamison Crowder";
passCatchers[3] = "Jordan Reed";
passCatchers[4] = "Matt Jones";
passCatchers[5] = "Chris Thompson";
int passCatcher = random.nextInt(passCatchers.length);
String[] backs = {"Matt Jones", "Chris Thompson"};
int runningBack = random.nextInt(backs.length);
String[] plays = new String[7];
plays[0] = "Pass complete to " + passCatchers[passCatcher]
+ " for " + numberOfYards + " yards.";
plays[1] = "Handoff to " + backs[runningBack] + " along the left
edge for " + numberOfYards + " yards.";
plays[2] = "Handoff to " + backs[runningBack] + " up the middle for
" + numberOfYards + " yards.";
plays[3] = "Handoff to " + backs[runningBack] + " along the right
edge for " + numberOfYards + " yards.";
plays[4] = "Screen pass to " + passCatchers[passCatcher] + " for "
+ numberOfYards + " yards";
plays[5] = "Kirk Cousins scrambles for " + numberOfYards + "
yards";
plays[6] = "Kirk Cousins sacked for " + lossOfYards + " yards.";
int select = random.nextInt(plays.length);
System.out.println(plays[select]);
totalYards += (numberOfYards - lossOfYards);
}
System.out.println("10 play drive. " + totalYards + " yards.")