This Works In My Other Code But Not This One?
I have been working on a ZORK type game in Java. I am a beginner/intermediate. Kind of right on the border. I think the problem is where the Else if(inputNumber == 9). If anyone sees anything wrong, just tell me.
Note that this is only one part of the code
do{
int inputNumber = keyboard.nextInt();
if(inputNumber == 0){
if(start = true){
out.println("YOU HAVE JUST LEFT DUNDEE MIDDLE SCHOOL.");
out.println("YOU ARE RIDING HOME ON THE BUS WHEN ALL");
out.println("OF A SUDDEN YOU SEE A MAJOR TRAFFIC JAM");
out.println("AW MAN! YOU THINK. BUT THEN YOU SEE THE");
out.println("CRATER IN THE GROUND. THE BUS DRIVER OPENS");
out.println("THE DOORS AND EVERYONE RUNS OUT. YOU RUN");
out.println("UNTIL YOU REACH YOUR HOME. ITS DESERTED.");
out.println("YOU REALIZE YOU HAVE TO FIGURE OUT WHAT");
out.println("IS GOING ON");
out.println("");
out.println("PRESS 9 TO GET YOUR OPTIONS");
}else
if(inputNumber == 9){
if(home == true){
out.println("PRESS 1 TO GO NORTH INTO HOUSE");
out.println("PRESS 2 TO GO EAST TO INTERSECTION");
out.println("PRESS 3 TO GO SOUTH TO PARK");
out.println("PRESS 4 TO GO WEST TO SHOP");
home = true;
Re: This Works In My Other Code But Not This One?
For me it's hard to tell what's wrong without more description of the mis-behavior and without seeing a bit more of the code.
From a practical coding standpoint, if this were my program I'd strive to separate the data from the code. For instance, the game map, instructions, and items found are mainly data, not code. In other words, try to make your code as flexible as possible so that it would allow you to be able to change the map and items, their location, etc, without having to change any code.
Re: This Works In My Other Code But Not This One?
Hi Ooogel
Please use [code=java] before your code and [/code] after your code.
You said you think there is a problem in a specific part of the code, but never said what the problem is. With the snippet provided there is no way to test the code to determine what (if any) the problem is. (As stated by curmudgeon)
If you still have a problem please post the question and code (inside tags!) again.
Re: This Works In My Other Code But Not This One?
Quote:
Originally Posted by
Ooogel
.
.
.
Code java:
do{
int inputNumber = keyboard.nextInt();
if(inputNumber == 0){
if(start = true){
out.println("YOU HAVE JUST LEFT DUNDEE MIDDLE SCHOOL.");
out.println("YOU ARE RIDING HOME ON THE BUS WHEN ALL");
out.println("OF A SUDDEN YOU SEE A MAJOR TRAFFIC JAM");
out.println("AW MAN! YOU THINK. BUT THEN YOU SEE THE");
out.println("CRATER IN THE GROUND. THE BUS DRIVER OPENS");
out.println("THE DOORS AND EVERYONE RUNS OUT. YOU RUN");
out.println("UNTIL YOU REACH YOUR HOME. ITS DESERTED.");
out.println("YOU REALIZE YOU HAVE TO FIGURE OUT WHAT");
out.println("IS GOING ON");
out.println("");
out.println("PRESS 9 TO GET YOUR OPTIONS");
}else
if(inputNumber == 9){
if(home == true){
out.println("PRESS 1 TO GO NORTH INTO HOUSE");
out.println("PRESS 2 TO GO EAST TO INTERSECTION");
out.println("PRESS 3 TO GO SOUTH TO PARK");
out.println("PRESS 4 TO GO WEST TO SHOP");
home = true;
By adding code tags to your original code so that I can check out your formatting, I think I may see a problem. Could be "the" problem, I'm thinking...
Anyhow, in spite of your indentation, the 'else' goes with the "if(start)", not with the "if(inputNumber == 0)"
Java compilers ignore indentation. And it's only convenient for Humans if the style results in something that is visually consistent with the logical organization.
Anyhow...
Here: Ill take your code with no changes other than a more spread-out style of indentation that is consistent with the way that the compiler sees it. (Well, I added some closing braces that will occur later on in your code, too.)
Code java:
do{
int inputNumber = keyboard.nextInt();
if(inputNumber == 0)
{
if(start = true)
{
out.println("YOU HAVE JUST LEFT DUNDEE MIDDLE SCHOOL.");
out.println("YOU ARE RIDING HOME ON THE BUS WHEN ALL");
out.println("OF A SUDDEN YOU SEE A MAJOR TRAFFIC JAM");
out.println("AW MAN! YOU THINK. BUT THEN YOU SEE THE");
out.println("CRATER IN THE GROUND. THE BUS DRIVER OPENS");
out.println("THE DOORS AND EVERYONE RUNS OUT. YOU RUN");
out.println("UNTIL YOU REACH YOUR HOME. ITS DESERTED.");
out.println("YOU REALIZE YOU HAVE TO FIGURE OUT WHAT");
out.println("IS GOING ON");
out.println("");
out.println("PRESS 9 TO GET YOUR OPTIONS");
} // Goes with if(start...)
else if(inputNumber == 9)
{
if(home == true){
out.println("PRESS 1 TO GO NORTH INTO HOUSE");
out.println("PRESS 2 TO GO EAST TO INTERSECTION");
out.println("PRESS 3 TO GO SOUTH TO PARK");
out.println("PRESS 4 TO GO WEST TO SHOP");
home = true;
.
.
.
}// Added by Zaphod_b: Goes with if (home == true)
.
.
.
} // Added by Zaphod_b: Goes with else if(inputNumber == 9)
} // Added by Zaphod_b: Goes with if(inputNumber == 0)
} while (Loop continuation boolean expression goes here); // Added by Zaphod_b: End of the do { stuff
Putting human-oriented comments on closing braces makes it easier for others to proofread and see if the code is consistent with your expressed intent. (Makes it easier for you to remember what you had in mind if you ever revisit the code for purposes of debugging or feature-enhancement or whatever.)
Bottom line (just a suggestion, really): If your text editor has features that let you see matching braces, turn on that feature. If it doesn't have that feature, find one that does. If your editor has an "autoindent" feature, turn it on and don't fight it. If it doesn't have that feature find one that does. You will thank me later.
Of course if that organization is not what you had in mind and I'm pretty sure it isn't, then maybe throw a closing '}' before the "else..." and unindent stuff below that or some such thing.
Hmmm...
What's the name of the reverse operation of "indent" ?
Is it really "unindent" ?
Why not "undent" ? (Shorter, more elegant.)
Or, better yet "outdent" ? (Logically consistent: "out" is opposite of "in," right?)
English is a lot more complicated and less precise than Java, right?
I mean, sometimes I feel as if I am in a maze of twisty little passages, all alike...
Cheers!
Z