Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (481)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (548)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [solved] if statement producing unusual results  (Read 354 times)
0 Members and 1 Guest are viewing this topic.
Offline happyfella

Senior Newbie


Projects: 1



« Posted 2013-09-02 06:25:00 »

I have a very newbish problem that I am sure will give you all a nice chuckle.  Smiley  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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
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
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
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)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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.
Offline happyfella

Senior Newbie


Projects: 1



« Reply #1 - Posted 2013-09-02 06:46:50 »

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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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");
      }
     
   }
Offline kramin42
« Reply #2 - Posted 2013-09-02 06:59:41 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
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?:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
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");
        }
    }
}

"All science is either physics or stamp collecting." - Ernest Rutherford.
CodeGolf4k
M4nkala
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nerb
« Reply #3 - Posted 2013-09-02 08:32:01 »

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.
Offline happyfella

Senior Newbie


Projects: 1



« Reply #4 - Posted 2013-09-02 18:08:13 »

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.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (26 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (14 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (60 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!