Java:最多可添加50并返回0

时间:2016-10-02 01:09:52

标签: java

我正在用Java创建一个足球游戏。驱动器从20码线开始,每次有播放时,码线都会更新。因此,如果第一场比赛是5码的增益,那么他们将在25码线上。问题是,比如他们在49码线上,他们得到一个10码的比赛。这应该把他们放在41码线。但是,我不知道如何做到这一点。我现在拥有自己的代码的方式,球将在59码线上,这在足球场上是不存在的。有什么建议吗?

public void callPlays(){

System.out.println("Redskins begin drive on their own 20 yard line");

Random random = new Random();
Random yardsGained = new Random();
Random yardsLost = new Random();

int numberOfYards = 0; // yards gained on play
int lossOfYards = 0; // yards lossed on sack
int down = 1;
int yards = 10;
int totalYards = 0;
int yardLine = 20;


while (true) {

  if (down == 1) {
    System.out.println(down + "st and " + yards + " on the Redskins " +    
    yardLine + " yard line.");

  } else if (down == 2) {
        System.out.println(down + "nd and " + yards + " on the Redskins 
        " + yardLine + " yard line.");

  } else if (down == 3) {
        System.out.println(down + "rd and " + yards + " on the Redskins 
        " + yardLine + " yard line.");

  } else if (down == 4) {
        System.out.println("Redskins punt");
        break;
  } 

  numberOfYards = yardsGained.nextInt(11);
  lossOfYards = yardsLost.nextInt(11);

  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] = "Cousins 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.";

        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int select = random.nextInt(plays.length);

        System.out.println(plays[select]);

        if (select == 6) {             
            yards += lossOfYards;
            totalYards -= lossOfYards;
            yardLine -= lossOfYards;
        } else {
            yards -= numberOfYards;
            totalYards += numberOfYards;
            yardLine += numberOfYards;
        }

        if(yards <= 0) {
            down = 0;
            yards = 10;
        }

        if(totalYards >= 80) {
            System.out.println("Touchdown!");
            break;
        }

        down++;

    }


}

}

5 个答案:

答案 0 :(得分:2)

我的建议是将足球场抽象为-50到50码,其中0为中心,+ -50为球门线。通过这种方式,您可以获得类似于整数的内容,可以轻松编写计算机编程语言。

当您向用户传达信息时,请调用一种方法将其转换为特定于域的语言 - 也就是说,人们期望足球场为0-50,50-0。

例如,如果某些事件发生在-41,你可以说这是“A队9码线”,或其他什么。

答案 1 :(得分:0)

这里最大的问题是“码线”不仅仅是一个数字。有两个40码线,所以如果yardLine是40,你怎么知道你在场上的位置?

执行此操作的“OO方式”(tm)将定义一个可以表示码线和场的哪一侧的类。我不会给你全班,但这里有一个概述它的样子:

public class YardLine {
    public YardLine(int yards, WhichTeam team) { ... }
    public void move(int yards, WhichTeam team) { ... }
    public String toString() { ... }
        // Could return something like "Skins' 25", "Opponents' 10", or 
        // maybe just "50-yard line" if that's where the ball is
}

WhichTeam代表了红皮队或其他球队。您可以通过将其设为intboolean来简化此操作,但我认为enum可能会导致代码更易读。 move意味着球队将球远离对手的目标移动(因此move(-5,REDSKINS)如果他们被解雇,则或多或少等同于move(5,OPPONENTS))。

至于实现类方法,我的建议是只保留一个范围从0到100的实例变量,并且在调用toString之前不要担心它所在的字段的哪一侧。这比试图保留另一个变量来告诉它所在的字段的哪一侧要容易得多。 (所以是的,内部,班级可能认为球在59码线上。但这是一个隐藏在callPlays()的实施细节。)变量范围从-50至50(如詹姆森的回答)是另一种可能性。

答案 2 :(得分:0)

有很多方法可以达到你想要的效果。如果你想坚持一个接近原始代码的解决方案(即没有重构一切都是面向对象的,这无疑是更经典的Java方法),处理它的一种方法可能就是简单地有两个{用于跟踪争球线的{1}}变量,一个从每个球队的终点区域的角度来看。

因此,您的代码可能以以下内容开头:

yardLine

int yardLine = 20; int opponentsYardLine = 80; 在您的结束区域为0,yardLine在其结束区域为0。在50码线,它们都是50。

每次出现故障时,您都需要更新两个变量,添加到一个变量并从另一个变量中减去变量。

opponentsYardLine

如果我们在你的问题中考虑我们从49码线开始并进行+10码游戏的例子,那么# make a +10 yard play int play = 10 yardLine += play opponentsYardLine -= play 将是59,这不是我们所说的那个位置领域。但是,yardLine将是41,这就是它的名称。

当您打印出自己的位置时,您可以单独处理opponentsYardLineyardLine < opponentsYardLineyardLine == opponentsYardLine == 50的情况。这也为您提供了一个可以修改print语句的位置,这样它就可以代替yardLine > opponentsYardLine来说明对方团队的名称。

也许是这样的:

" on the Redskins "

答案 3 :(得分:0)

你决定实施这个的方式很奇怪。有很多方法可以做到这一点。我能为您提供当前代码的最佳方法就是这样。首先,不是让两个单独的变量(lossOfYards和numberOfYards)创建一个(即changeInYards),然后功能(可能不高效)代码如下。

while (changeInYards != 0){
    if (changeInYards < 0){
        totalYards--;
        if(totalYards > 50){
            yardLine++;
        }else{
            yardLine--;
        }
        changeInYards++;
    }else {
        totalYards++;
        if(totalYards > 50){
            yardLine--;
        }else{
            yardLine++;
        }
       changeInYards--;
    }
}

如果玩家超过50码线以及前进的方向,此代码将检查获得的每个码数。根据该信息,我们相应地更改变量。

答案 4 :(得分:-3)

if(yardLine > 50) { yardLine = 100 - yardLine; }