Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: dr java

  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question dr java

    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"


    import java.io.*;
    class SlotGame
    {
    public static void main (String[] args) throws IOException
    {
    BufferedReader myInput = new BufferedReader (new InputStreamReader(System.in));

    String inName;
    String inanswer1;
    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 ();
    if (inanswer1.equals("y"));
    System.out.println ("Lets Play!");
    else if (inanswer1.equals("n"))
    System.out.println ("GAME OVER");


    }
    }

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Posts
    1,776
    Thanks
    41
    Thanked 196 Times in 195 Posts
    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 ";"
    Code:
    if (inanswer1.equals("y"));
    System.out.println ("Lets Play!");
    else if (inanswer1.equals("n"))
    System.out.println ("GAME OVER");
    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()-

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 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:
    PHP Code:
    Scanner myInput = new Scanner(System.in);
    ...
    inName myInput.nextLine(); 


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •