Random 如何获得';对于循环';

Random 如何获得';对于循环';,random,Random,我正在创建一个程序,创建一个10人一组的足球赛。我有三个随机对象。其中一个随机抽取玩家进行游戏。当四分卫被解职时,第二个随机数为正收益,第三个随机数为负收益 我的问题是,我希望能够在环路结束后计算出驾驶的总码数(正码-负码),但我无法正确计算这些数字。有什么想法吗 公共图书馆{ Random Random=new Random();//播放和播放 Random Yardsgened=new Random();//正面播放 Random yardsLost=new Random();//负面播放

我正在创建一个程序,创建一个10人一组的足球赛。我有三个随机对象。其中一个随机抽取玩家进行游戏。当四分卫被解职时,第二个随机数为正收益,第三个随机数为负收益

我的问题是,我希望能够在环路结束后计算出驾驶的总码数(正码-负码),但我无法正确计算这些数字。有什么想法吗

公共图书馆{

Random Random=new Random();//播放和播放
Random Yardsgened=new Random();//正面播放
Random yardsLost=new Random();//负面播放
整数=0;
整数码=0;
int lossOfYards=0;
System.out.println(“红人开始在自己的20码线上行驶”);

对于(int i=0;i,在您的例子中,似乎您只获得上一次迭代的yardsgened和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.")
public void callPlays(){
Random Random=新建Random();//播放和播放
Random Yardsgened=new Random();//正面播放
Random yardsLost=new Random();//负面播放
整数=0;
整数码=0;
int lossOfYards=0;
System.out.println(“红人开始在自己的20码线上行驶”);

对于(int i=0;iI赢得和输掉的赌注每次都返回相同的数字,因为他们可能会有相同的种子。我已经玩了一点。他们都返回不同的数字。问题是,赢得和输掉的赌注在循环的每个系列中都抛出一个数字,所以输掉的赌注会从赢得的每个系列中减去,所以在t结束时他玩10次驾驶,数学不正确。我想让“迷失”在玩时只抛出一个数字[6]被调用。不知道如何实现。感谢您的反馈,但它仍然不起作用。似乎正在发生的是,lossOfYards在循环的每一轮中都会抛出一个数字,因此很明显,总码数与调用的比赛中获得和失去的码数不一致。我希望lossOfYards只抛出一个数字r当选择播放[6]时,我不知道如何做到这一点。
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.")