Results 1 to 9 of 9
  1. #1
    kalaiss is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Help with If/Else not working correctly?

    Good morning, thanks for having a look at this.

    Basically when I enter my nextBoolean value as true, the code works as intended. But when I enter a false value, the if(firstbat==false) won't work. I've also tried using an else statement but still no go.

    heres the code and thanks very much
    Java Code:
    			if(firstbat==true)
    				{
    				System.out.println(""+ namep1 + " is batting first.");
    				System.out.println("How many innings should be played, 1 or 2?");
    				noInnings=sc.nextInt(3);
    				System.out.println("You have chosen to play " + noInnings + " innings.");
    				System.out.println("Player 1, " + namep1 +", is up to bat! This is his first innings");
    				
    				rngTool = generator.nextInt(11); // vehicle type 9 parts car 1 part vehicle
    				if(rngTool>9)
    					{
    						rngTool= generator.nextInt(4);
    						switch(rngTool)
    							{
    								case 0:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is an SUV, the colour is Red. 6 runs are added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is an SUV, the colour is not Red. 1 run is added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 1;
    										break;
    									}
    								break;
    						
    								case 1:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is a Van, the colour is Red. 6 runs are added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is a Van, the colour is not Red. 1 run is added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 1;
    										break;
    									}
    								break;
    						
    								case 2:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is a Truck, the colour is Red. 6 runs are added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is a Truck, the colour is not Red. 1 run is added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 1;
    										break;
    									}
    								break;
    							
    								case 3:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is a Bus, the colour is Red. 6 runs are added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is a Bus, the colour is not Red. 1 run is added to "+namep1+"'s innings total.");
    										scoreP1I1 = scoreP1I1 + 1;
    										break;
    									}
    								break;
    							}
    				System.out.println("score is "+scoreP1I1+".");
    					}
    				
    			else
    				{
    				System.out.println(""+ namep2 + " is batting first.");
    				System.out.println("How many innings should be played, 1 or 2?");
    				noInnings=sc.nextInt(3);
    				System.out.println("You have chosen to play " + noInnings + " innings.");
    				System.out.println("Player 2, " + namep2 +", is up to bat! This is his first innings");
    				
    				rngTool = generator.nextInt(11); // vehicle type 9 parts car 1 part vehicle
    				if(rngTool>9)
    								{
    						rngTool= generator.nextInt(4);
    						switch(rngTool)
    							{
    								case 0:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is an SUV, the colour is Red. 6 runs are added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is an SUV, the colour is not Red. 1 run is added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 1;
    										break;
    									}
    								break;
    						
    								case 1:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is a Van, the colour is Red. 6 runs are added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is a Van, the colour is not Red. 1 run is added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 1;
    										break;
    									}
    								break;
    						
    								case 2:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is a Truck, the colour is Red. 6 runs are added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is a Truck, the colour is not Red. 1 run is added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 1;
    										break;
    									}
    								break;
    							
    								case 3:
    								rngTool = generator.nextInt(13);
    								switch(rngTool)
    									{
    										case 0:
    										System.out.println("The vehicle is a Bus, the colour is Red. 6 runs are added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 6;
    										break;
    								
    										default:
    										System.out.println("The vehicle is a Bus, the colour is not Red. 1 run is added to "+namep2+"'s innings total.");
    										scoreP2I1 = scoreP2I1 + 1;
    										break;
    									}
    								break;
    							}
    				System.out.println("score is "+scoreP2I1+".");
    					}
    thank you in advance
    Last edited by pbrockway2; 04-16-2012 at 02:48 AM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Help with If/Else not working correctly?

    That's a lot of code that you're asking us to go through -- can you post a response below that pares the code down to the necessary essentials? Also please use [code] [/code] tags so that it remains formatted. Finally, there's no need to do

    Java Code:
    if (foo == true) {
    
    } else if (bar == false) {
    
    }
    As it's much simpler and cleaner to just do:

    Java Code:
    if (foo) {
    
    } else if (!bar) {
    
    }

  3. #3
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Help with If/Else not working correctly?

    I don't see any nextBoolean or if (firstbat == false) in the code. Also, what do you mean by it won't work?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with If/Else not working correctly?

    Hi kalaiss, welcome to the forums!

    A couple of things that may help you get speedier help: first use the "code" tags when you post code. Put [code] at the start of the code and [/code] at the end. That way formatting gets preserved. Of course, to be effective in making your code readable indents should be kept to a moderate size: I would suggest 4 spaces (and no tabs) per indent level.

    Secondly, people reading the post will have no idea what "won't work", "no go" and similar mean. You ought to describe both the intended behaviour and the behaviour you observe when the program is run. Likewise an expression like "when I enter my nextBoolean value as true" suggests that there is a nextBoolean variable involved, but it does not occur in the posted code snippet. And, in the absence of code, we can only guess what it means to enter a variable's value.

    Consider reposting with code that is complete - ie others can compile and run it. Keep it short. There's a huge switch statement in there: if it's not part of the problem, remove it. Basically it would help if you were to construct a small program that illustrates the problem you are having, and accompany it with a description of what that problem is (intended vs actual runtime behaviour).
    Last edited by pbrockway2; 04-16-2012 at 03:02 AM.

  5. #5
    kalaiss is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with If/Else not working correctly?

    Thanks everyone for the advice

    I did try that exact if statement by the way with the same result.

    That result (not working) was when I input my nextBoolean value, if I put in true, the code continued to play a lovely game of car cricket. If I entered a false value, rather than continuing with the code under if(firstbat==false), it would simply say "Process Completed" and end the program.

    Using jcreator pro if that helps

    Cheers again

  6. #6
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Help with If/Else not working correctly?

    I'm not familiar with jcreator pro or your skills with the IDE. Are you debugging the program using breakpoints?

  7. #7
    kalaiss is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Help with If/Else not working correctly?

    Quote Originally Posted by shall View Post
    I'm not familiar with jcreator pro or your skills with the IDE. Are you debugging the program using breakpoints?
    there are no break points as such, just incorrect output, and my skills are limited :p

    one other issue im having that is probably alot easier to solve is the following
    Java Code:
    					switch(noInnings)
    						{
    							case scoreP1T == scoreP2T:
    							System.out.println("The players scores are equal! The game is a draw.");
    							break;
    							
    							case scoreP1T > scoreP2T:
    							System.out.println(""+namep1+" scored more runs than "+namep2+". Player 1 is the winner!.");
    							break;
    							
    							case scoreP2T > scoreP1T:
    							System.out.println(""+namep2+" scored more runs than "+namep1+". Player 2 is the winner!.");
    							break;
    						}
    				}
    bugs saying that im using booleans instead of int's but ive checked by instantiations and they all seem correct.

    Any ideas?

  8. #8
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    199
    Rep Power
    0

    Default Re: Help with If/Else not working correctly?

    I seriously suggest you use Netbeans (http://netbeans.org/downloads/) - select the "All" column. I only have part of program, so I'm not able to test it.

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Help with If/Else not working correctly?

    If you can't understand a compiler message, post it and say to which line(s) of your code it is referring.

Similar Threads

  1. method not working correctly
    By r1b in forum New To Java
    Replies: 4
    Last Post: 01-04-2012, 08:09 PM
  2. Replies: 2
    Last Post: 12-28-2011, 09:15 AM
  3. Applet not working correctly?
    By Beavotropper2 in forum Java Applets
    Replies: 2
    Last Post: 04-18-2011, 06:32 AM
  4. Error Checking not working correctly
    By RickAintree in forum New To Java
    Replies: 1
    Last Post: 12-15-2010, 01:54 PM
  5. [SOLVED] \t not working correctly?
    By Gakusei in forum New To Java
    Replies: 5
    Last Post: 05-06-2008, 04:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •