Help with simple text game...
Hello gang,
I am looking for some direction - I can program but I am an industrial Engineer and so I program Electronics such as PICs and also lots of PLC Software... I have no Java experience but I used to be quite good with Fortran!
My son has come home from school and he wants to build a decision based text game and so I have committed to assist him build it in Java >.< So I need some pointers - I will be using Netbeans and have a rough understanding of the bare bones of a simple Java program (Hello World!!)
So here is my first question:
I would like to print one of several responses onto the screen based on an answer that I give to a question ie.
Question: You have entered a room, there are 3 doors please choose a door to enter: a) Door to the left, b) Door straight ahead, c) Door to the right
I then input a, b or c and then some text will be printed to the screen.
Please help - Ian
Re: Help with simple text game...
You're going to need a way of writing lines on the console, which comes for free with System.out - it's an object of type java.io.PrintStream, so you can println(String) to write some text onto the console followed by a newline. Next up, you'll need to read a line (the easiest way) of input from the user. That's not quite so easy, so open wide for some spoonfeeding: "new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine()"* will return a String object that will hold what the user entered, without the newline.
Do you have access to the Java SE API documentation? There's a lot to read, but it's well worth reading. Start with the words in CamelCase above.
Java Platform SE 6
*I think the kids on the lawn outside may all be using Console.readLine() and Console.writer().println() nowadays.
Re: Help with simple text game...
Okay - this is what I got to so far:
Code :
package text.project;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Ian Smith
*/
public class TextProject {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
System.out.print("Enter your name and press Enter: ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String name = null;
try {
name = br.readLine();
} catch (IOException e) {
System.out.println("Error!");
System.exit(1);
}
System.out.println("Your name is " + name);
}
}
Now I want to put some if statements in - but I don't know how to go about it!!! Any help appreciated as always - Ian
Re: Help with simple text game...
Quote:
Originally Posted by
iansmiler
Now I want to put some if statements in - but I don't know how to go about it!!! Any help appreciated as always - Ian
Please use highlight tags when posting code.
You're going to want to get into the habit of checking the basic tutorials before asking a question- it's faster for you, and it's easier to help you when you get stuck or have a specific question than it is to answer "how do I do this" type questions.
Your new best friend, which is the first result for googling java tutorials: The Java™ Tutorials
Of particular interest here, which is the first result for googling java if statements: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Help with simple text game...
Always use Exception.printStackTrace() in place of print("An error occurred!"). Java if statements look like
Code java:
if ("Ian".equals(name))
System.out.println("Sifu");
else
System.out.println("Hello, " + name);
Java has better help for this sort of thing than I can give you:
Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Re: Help with simple text game...
Thanks guys - more exploration for me to do :D