Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java Problem: My Program Keeps Terminating For No Reason?
Okay, so I was making but I encountered a problem. I wanted to make a text based game, so first I need to make a main menu. But for whatever reason, my little beginnings of a game are not working! No matter which option I type in, it just doesn't work. Eclipse says "<terminated>". It won't even print out what I tried to make it, when I choose option two. I even tried removing "System.exit(0)", or changing the zero to different things, or removing it all together. Maybe you can try running it and see what you get. Here's the code:
import static java.lang.System.*;
import java.util.Scanner;
public class TextTale
{
public static void main(String[] args)
{
out.println(" TextTale ");//Main Menu.
out.println(" 1.play ");//Option to play game.
out.println(" 2.stats ");//Option to view stats.
out.println(" 3.exit ");//Option to exit the game.
Scanner myScanner=new Scanner(System.in);
int MenuInput=myScanner.nextInt();
if(MenuInput==1)
/*
* begins adventure or picks up where they left off.
*/
if(MenuInput==2)
{
out.println("This is a test.");
/*
* print out the player stats.
*/
}
if(MenuInput==3)
{
System.exit(0);
}
}
}
So now that you see the code, can you help me? And, if you are feeling awfully generous, how exactly would I make a method call to another class file so that I can, for instance, begin to play the game. And if your feeling more generous, how do I make an inventory? Then as a final favor for those who are REALLY generous, how do I save the players stats (they will have health, levels, equipped weapons, inventory, etc.), and then read them back into the program once it's reopened? Now this last little paragraph isn't required, though, from a novice to all of the awesome people out there, it would really help me more then you can imagine! After all, we've got to start somewhere, eh? I know you guys probably feel similar when you learn a new programming language.
Thank you Jake! Or is that Lake? I didn't notice that I forgot the curly braces! And unlike some novices, I wouldn't copy and paste the code. I'm doing this for educational purposes. So I'd take what I learn from what you would've shown me, and try to figure out how it works, etc. But I understand why you would think that. And so anyone out there that stumble upon this from now on, can you focus on the last paragraph? Also, about the readable problem, that jake/lake mentioned, you could make it in a text file, then put it on mediafire for download, or something, or make a torrent out of it. Or at least give me helpful tutorials on how to do all of those things. Thank you.
1 Answer
- jakeLv 49 years agoFavorite Answer
I assume you stripped some of the code from what was posted above?
Because there are logic errors with your syntax. The first if statement includes the second, such that the second is only called if the first 'if' results in 'true'. So "this is a test" will only appear if 'MenuInput' is 1 AND 2 at the same time, which is impossible.
In essence, your program can only do one thing... exit. if user types 1, it will exit the main() function. If user types 2, it will exit the main() because the test for option #2 is nested inside the test for option #1, therefore it is unreachable code. And of course if they type #3, it performs system.exit.
This:
if(MenuInput==1)
/*
* begins adventure or picks up where they left off.
*/
Should be this:
if(MenuInput==1) {
/*
* begins adventure or picks up where they left off.
*/
}
Notice the "curly braces" { and }
Aside from that, you appear do be doing the System.in properly.
In terms of completing some of the code requests you have stated, it would be very difficult to post that into here without compiling it and testing it for errors... because yahoo answers does not provide [code] tags for us to provide formatting that is humanly readable. Therefore it would only be possible for me to fully write a working set of code and create a massive reply with all of the code such that you could copy and paste it into your java project. Which defeats the purpose of learning! :)
If you truly need some source code examples to assist you in learning this, other than using Google to find similair scenarios, you can contact me directly here in yahoo, and I can email you a github post or text file.