[solved] if statement producing unusual results

I have a very newbish problem that I am sure will give you all a nice chuckle. :slight_smile: This is the first time I have posted on a Dev forum, kinda nervous. haha

The issue I am running into is with the if statement within the Intro Class. With the current setup this if statement will always return ā€œwinnerā€ 100% of the time but if I use the == operator it will provide different results dependent on the Player.player.traitOneID and the argument value. I need the > operator to work correctly to produce the desired results but it is not.

The traitOneID is setup almost like the players level, this is so I can run a random number against the traitOneID and it will give a conclusion dependent on how high the level is. I was originally using this particular if statement to see if the variable player in the Player Class was actually being updated, which it is, and then ran across this issue which left me puzzled. Side note, I am reaching this if statement by only pressing the 1 key on the keyboard

Player Class

public class Player {
	
	public static Player player;

	public String name;
	public String position;
	public String history;
	public String traitOne;
	public String traitTwo;
	public int traitOneID;
	public int traitTwoID;	

	
	public static Player playerOne(){
		Player playerOne = new Player();
		playerOne.name = "1. Player One";
		playerOne.position = "Science Officer";
		playerOne.history = "A brief background of Player One";
		playerOne.traitOne = "Intelligence";
		playerOne.traitOneID = 75;
		playerOne.traitTwo = "Courage";
		playerOne.traitTwoID = 25;
		return playerOne;
	}
	
	public static Player playerTwo(){
		Player playerTwo = new Player();
		playerTwo.name = "2. Player Two";
		playerTwo.position = "Pilot";
		playerTwo.history = "A brief background of Player Two";
		playerTwo.traitOne = "Intelligence";
		playerTwo.traitOneID = 50;
		playerTwo.traitTwo = "Courage";
		playerTwo.traitTwoID = 50;
		return playerTwo;
	}
	
	public static Player playerThree(){
		Player playerThree = new Player();
		playerThree.name = "3. Player Three";
		playerThree.position = "Combat Officer";
		playerThree.history = "A brief background of Player Three";
		playerThree.traitOne = "Intelligence";
		playerThree.traitOneID = 25;
		playerThree.traitTwo = "Courage";
		playerThree.traitTwoID = 75;
		return playerThree;
	}
}

PlayerSelect Class

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{
		Input userInput = gc.getInput();
		if(userInput.isKeyPressed(Input.KEY_1)){
			Player.player = Player.playerOne();
			sbg.enterState(2);
		}
		if(userInput.isKeyPressed(Input.KEY_2)){
			Player.player = Player.playerTwo();
			sbg.enterState(2);
		}
		if(userInput.isKeyPressed(Input.KEY_3)){
			Player.player = Player.playerThree();
			sbg.enterState(2);
		}
	}

Intro Class (here is the if statement in question)

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(100);
	    for (int i = 1; i <= 10; ++i)
		if(Player.player.traitOneID > randomInt){
			System.out.println("winner");
		}else{
			System.out.println("loser");
		}
		
	}

I hope I have included all the code needed and explained thoroughly so you can understand my issue, any help in furthering my knowledge would be greatly appreciated.

I figured out the solution. Needed the >= operator for this to function properly instead of just >. Newb move by a newb programmer :wink:

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
		Random randomGenerator = new Random();
		int randomInt = randomGenerator.nextInt(100);
	   for (int i = 1; i <= 10; ++i)
		if(Player.player.traitOneID >= randomInt){
			System.out.println("winner");
		}else{
			System.out.println("loser");
		}
		
	}

I’m not sure if that’s your problem. The ā€˜=’ case shouldn’t make much difference. I think your problem is with your ā€˜for’ loop:

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
      Random randomGenerator = new Random();
      int randomInt = randomGenerator.nextInt(100);
      for (int i = 1; i <= 10; ++i)
      if(Player.player.traitOneID >= randomInt){
         System.out.println("winner");
      }else{
         System.out.println("loser");
      }
      
   }

In its current state it simply does the same thing 10 times without anything changing between those times. Did you mean to have something like this?:

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{
    Random randomGenerator = new Random();
    int randomInt;
    for (int i = 1; i <= 10; ++i){
        randomInt = randomGenerator.nextInt(100); // <-- get a new random integer
        if(Player.player.traitOneID >= randomInt){
            System.out.println("winner");
        }else{
            System.out.println("loser");
        }
    }
}

Kramin has hit the nail on the head. Your current loop just compares traitOneID to an unchanging randomInt ten times. Hence your answer will be the same for every iteration of the loop. The ā€˜=’ in the ā€˜>=’ operator just makes it inclusive of randomInt, whilst ā€˜>’ is exclusive.

And onto a couple of other things… I strongly suggest you wrap your ā€˜for’ block in braces, do not just leave it open like that (see kramin’s code for an example). It may also pay if you read up a little bit on the relational operators; I’m not certain, but I’ve got a feeling you are somewhat new to them? The java tutorials are a good place to start: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html but you may be able to find something more in-depth if you Google ā€˜relational operators’.

Cheers,
nerb.

Thank you both for your replies, they are very helpful. Nerb thank you for the braces insight, also I thought my understanding of operators was sound but you are correct that I am very new at this, so I will go back and review.

Side note, this for loop will continually run producing winner and loser as desired but I need it to only run a single random number to compare it to the traitOneID. I will do my research into this, as I thought my for loop understanding was sound as well. Maybe a for loop isn’t the best option.