samedi 29 juillet 2017

Why does my compiler give me some things one time and others another time without me changing the code

Using my previous question's code I have one question. Why does my compiler: http://www.browxy.com/ sometimes say what I want it to:

Amount of Players total: 11
Amount of Players going up against you: 5
Your team: team2

And other times say:

Amount of Players total: 7
Amount of Players going up against you: 3
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Game.main(Game.java:110)

If it helps, all of my code is here:

import java.util.Random;

public class Game {
public static void main(String[] args) {
Random m = new Random();
int b;
b = m.nextInt(12);
if (b >= 6) {
  b = b;
} else {
  b = b + 6;
}
int a = b;
int d;
d = m.nextInt(1);
int playerAmount = a;
int c = playerAmount / 2 + d;
System.out.println("Amount of Players total: " + playerAmount);
System.out.println("Amount of Players going up against you: " + c);
int blur;
blur = m.nextInt(3);
int cdp;
cdp = m.nextInt(3);
int red;
red = m.nextInt(3);
if (blur == 0) {
  blur = blur + 1;
}  else if (cdp == blur) {
  cdp = cdp - 1;
}  else if (cdp == 0) {
  cdp = cdp + 1;
}  else if (cdp == blur) {
  cdp = cdp - 1;
} else {
  cdp = cdp;
}
int player;
player = m.nextInt(3);
if (player == blur) {
  player = blur;
} else if (player == red) {
  player = red;
} else {
  player = cdp;
}
if (player == cdp) {
  player = cdp;
} else {
  player = cdp;
}
if (blur == 1) {
  blur = 1;
} else if (cdp == blur) {
  cdp= cdp - 1;
} else {
  cdp= cdp;
}
if (cdp == blur) {
  cdp = cdp - 1;
} else if (cdp == 0) {
  cdp = cdp;
} else {
  cdp = cdp + 2;
}
if (red == blur) {
  red = red + 1;
} else if (red == cdp) {
  red = red + 1;
} else {
  red = red;
}
if (red == cdp) {
  red = red + 1;
} else if (red == blur) {
  red = red + 1;
} else {
  red = red;
}
if (blur >= 0) {
  blur = blur - 1;
} else if (cdp == 0) {
  cdp = cdp + 1;
} else if (red >= 0) {
  red = red - 1;
}
if (red == cdp) {
  red = red + 1;
} else if (red == blur) {
  red = red + 1;
} else {
  red = red;
}
if (red == blur) {
  red = red + 1;
} else if (red == cdp) {
  red = red + 1;
} else {
  red = red;
}
if (cdp == blur) {
  cdp = cdp + 1;
} else if (red == cdp) {
  cdp = cdp + 1;
} else if (red == blur) {
  red = red - 1;
} else {
  red = red;
}
String[] teams = {"blur", "cdp", "red"};
System.out.println("Team1: " + teams[cdp]);
System.out.println("Team2: " + teams[blur]);
System.out.println("Team3: " + teams[red]);
System.out.println("You are on Team " + teams[player]);
System.out.println("Team1 (blur): " + blur);
System.out.println("Team2 (cdp): " + cdp);
System.out.println("Team3 (red): " + red);
}
}

I know it might be a lot of code but all my question is why does my compiler give me some things one time and others another time without me changing the code




Aucun commentaire:

Enregistrer un commentaire