Results 1 to 9 of 9
- 04-16-2012, 02:37 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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
thank you in advanceJava 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+"."); }Last edited by pbrockway2; 04-16-2012 at 02:48 AM. Reason: code tags added
-
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
As it's much simpler and cleaner to just do:Java Code:if (foo == true) { } else if (bar == false) { }
Java Code:if (foo) { } else if (!bar) { }
- 04-16-2012, 02:57 AM #3
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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?
- 04-16-2012, 02:58 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 04-16-2012, 03:37 AM #5
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
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
- 04-16-2012, 04:16 AM #6
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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?
- 04-16-2012, 05:01 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 3
- Rep Power
- 0
Re: Help with If/Else not working correctly?
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
bugs saying that im using booleans instead of int's but ive checked by instantiations and they all seem correct.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; } }
Any ideas?
- 04-16-2012, 05:16 AM #8
Senior Member
- Join Date
- Apr 2012
- Posts
- 199
- Rep Power
- 0
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.
- 04-16-2012, 06:32 AM #9
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Similar Threads
-
method not working correctly
By r1b in forum New To JavaReplies: 4Last Post: 01-04-2012, 08:09 PM -
Trying to use the Paint but program isn't working correctly
By quickfingers in forum New To JavaReplies: 2Last Post: 12-28-2011, 09:15 AM -
Applet not working correctly?
By Beavotropper2 in forum Java AppletsReplies: 2Last Post: 04-18-2011, 06:32 AM -
Error Checking not working correctly
By RickAintree in forum New To JavaReplies: 1Last Post: 12-15-2010, 01:54 PM -
[SOLVED] \t not working correctly?
By Gakusei in forum New To JavaReplies: 5Last Post: 05-06-2008, 04:45 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks