Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Thread: dr java
01-17-2012, 09:21 PM #1
- Join Date
- Jan 2012
- Thanked 0 Times in 0 Posts
Im trying to make a a program that operates almost like a slot machine and this is what i have so far. im having a bit of trouble with the "if" statments so can someone tell me what i did wrong. also it wont let me add "else if" to the second "if"
public static void main (String args) throws IOException
BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));
System.out.println ("Please Input Your Name");
inName = myInput.readLine ();
System.out.println (inName + ",Welcome to the Slot Game!");
System.out.println (inName + " Would you like to play?(y/n)");
inanswer1 = myInput.readLine ();
System.out.println ("Lets Play!");
else if (inanswer1.equals("n"))
System.out.println ("GAME OVER");
01-18-2012, 12:26 AM #2
i don't know if java supports readLine() like C# (I thought it didn't- you have implamented it like C#)
also- and if the way you did it is accurate - then your if statement issues are due to a lone ";"
Edit: I also did not check to see if the implamentation of inanswer1.equals("") was done correctly- I am only looking @ your if statement and noting the readLine() -bc I thought Java did not support readLine()-Code:if (inanswer1.equals("y")); System.out.println ("Lets Play!"); else if (inanswer1.equals("n")) System.out.println ("GAME OVER");
01-18-2012, 04:12 AM #3
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,660 Times in 2,629 Posts
ReadLine exists in Java's buffered reader, and the equals is also fine (comparisons of java strings should only ever be done via methods such as .isempty or .equals, and never with ==).
The suggestion I have is to abandon the bufferedreader and use the scanner instead as its a lot easier to work with:
Scanner myInput = new Scanner(System.in);
inName = myInput.nextLine();