#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Lightbulb 21 Sticks...help with code


    Hello,

    I've just started learning to program and have begun with Java. I'm working my way through an online tutorial and after the beginner section (before the intermediate section) there are some games to create as practice; this is the third one.

    I've followed all the steps and created the game successfully and now I'm adding to it in order to practice my skills.

    I decided to amend the following:

    (1) Amend the output so that if there is only 1 stick, the output is "There is 1 stick" instead of "There are 1 sticks"

    (2) Close all open scanners

    (3) Amend the output so that if the computer takes 1 stick, the output is "Computer takes 1 stick" instead of "Computer takes 1 sticks"

    (4) Amend the opening sequence to include the title of the game (i.e. figure out how to put string into "") and ask the user if they want to play. If yes then play the game and if not then say goodbye and don't play.

    I have done everything up until ending the game if the user doesn't want to play.

    I have tried:

    Adding break but I keep being told this cannot exist outside of a loop etc.

    The opening of the code is:

    Code:
    package games.javatutorials;
    
    import java.util.Scanner;
    
    public class TwentyOneSticks {
    	
    public static void main(String[] args) {
    	
    int numSticks = 21;
    int numToTake = 0;
    
    Scanner reply = new Scanner (System.in);
    Scanner input = new Scanner (System.in);
    Scanner take = new Scanner (System.in);
    
    System.out.println("The game is \"21 Sticks\", are you ready to play? (Y/n)");
    String ready = reply.nextLine();
    
    if (ready.equals("y") || ready.equals("Y")) {
    	System.out.println("Let's play!");}
    
    //I tried adding: else {
    // System.out.println("Ok, goodbye!");
    // break;
    // }
    
    // This obviously did not work. I also tried adding the break
    // at the end of my while loop (as though part of the above if
    // statement which did not work.
    
    System.out.println("Would you like to go first? (Y/n)");
    String goFirst = input.nextLine()
    The game works fine i.e. the message "let's play" is printed if I say Y or y and is not printed if I choose anything else but I don't want the game to play if I choose anything other than Y or y.

    I'm probably jumping ahead i.e. I'm sure the tutorial will teach me this soon but I'd like to know if there's a simple way of doing this. Please keep in mind that I have only learned up to the beginner stuff in:

    Java made easy dot com

    So please keep the solution as simple and basic as possible.

    Thank you very much,

    3773
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I don't want the game to play if I choose anything other than Y or y.
    If you want the program to exit, call the System.exit(0); method.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Thumbs up


    Originally Posted by NormR
    If you want the program to exit, call the System.exit(0); method.
    Hello there and thanks for taking the time to reply!

    As you can imagine, I have investigated this code before implementing it and found that most people agree this should only be used for errors and to shut down the program completely. I don't want to do that and so I should probably specify that although I'm after a simple solution, I do want it to be the right one - can you or someone else possibly elaborate on why system.exit() would be the best solution here? If there's another solution, can some advice be offered about that too?

    Thank you,

    An example site regarding this code:

    javapractices dot com/ topic/ TopicAction.do?Id=86
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    I don't want the game to play
    Explain what you mean by that? What should the program do when "you don't want the game to play"?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Thumbs up


    Originally Posted by NormR
    Explain what you mean by that? What should the program do when "you don't want the game to play"?
    Hello, I've explained what I want the program to do, my point was that my investigation of your suggestion tells me that I shouldn't use System.exit(0) as it's meant for serious errors and can cause issues if you want to use your class again as part of a bigger program.

    I'm pleased to say that I found a solution by basically rewriting the program a bit and ended up using a solution that previously didn't work because I have now moved things around a bit however, there is something I don't understand and I will post this as a separate reply as I don't want to move on until I fully understand everything that's happening with my program.

    Cheers,
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Question


    Hello,

    So I have gotten my game to do what I want it to do now (which is great), I basically just added the "else" bit to the bottom of the code and moved something inside the "if" section at the top.

    What I don't understand though is why the scanners are closed?

    I thought these had to be closed within the correct brackets? I'm not sure how to ask that so I'll try to be clearer:

    All my previous programs only closed the scanner once I added "nameofscanner.close()" to the bottom of the code within the same bracket as the top e.g.

    code here {

    more code{

    Scanner new = new Scanner....etc.

    }

    ...
    ...
    ...

    and at the bottom after many open and closed brackets I had to add "new.close()" here:

    }
    new.close()
    }

    As you can see from this program, it looks like I should have added the close bits at the same place as where "else" currently is at the bottom because there are 2 brackets at the top before I open my scanners yet the commands to close them are within 3 brackets at the bottom. This had to be exact in my other programs (the location where I closed the scanners) so does anyone know why I am seemingly able to close them in a different place this time?

    I hope my question is clear enough!

    Code:
    package games.javatutorials;
    
    import java.util.Scanner;
    
    public class TwentyOneSticks {
    	
    public static void main(String[] args) {
    	
    int numSticks = 21;
    int numToTake = 0;
    
    Scanner reply = new Scanner (System.in);
    Scanner input = new Scanner (System.in);
    Scanner take = new Scanner (System.in);
    
    System.out.println("The game is \"21 Sticks\", are you ready to play? (Y/n)");
    String ready = reply.nextLine();
    
    if (ready.equals("y") || ready.equals("Y")) {
    	System.out.println("Let's play!");
    	System.out.println("Would you like to go first? (Y/n)");
    	String goFirst = input.nextLine();
    
    while (numSticks>0) {
    	if (goFirst.equals("y") || goFirst.equals("Y")) {
    		if (numSticks > 1) {
    			System.out.println("There are " + numSticks + " sticks");
    		}
    		else {
    		System.out.println("There is 1 stick");
    		}
    		System.out.println("How many sticks to take? (1 or 2)");
    		numToTake = take.nextInt();
    		if (numToTake<1) {
    		numToTake=1;
    		}
    		else if (numToTake>2) {
    		numToTake=2;
    		}
    		numSticks = numSticks - numToTake;
    		if (numSticks<=0) {
    			System.out.println("You lose!");
    		}
    		else {
    			if ((numSticks-2) % 3 == 0 || numSticks - 2 == 0) {
    			numToTake = 1;
    			}
    			else {
    			numToTake = 2;
    			}
    			if (numToTake > 1) {
    			System.out.println("Computer takes " + numToTake + " sticks");
    			}
    			else {
    			System.out.println("Computer takes " + numToTake + " stick");
    			}
    			numSticks = numSticks - numToTake;
    			
    			if (numSticks <=0) {
    				System.out.println("You win!");
    			}
    		}
    	}
    	else {
    		if ((numSticks-2) % 3 == 0 || numSticks - 2 == 0) {
    			numToTake = 1;
    		}
    		else {
    			numToTake = 2;
    		}
    		if (numToTake > 1) {
    			System.out.println("Computer takes " + numToTake + " sticks");
    		}
    		else {
    			System.out.println("Computer takes " + numToTake + " stick");
    		}
    		numSticks = numSticks - numToTake;
    		
    		if (numSticks <=0) {
    			System.out.println("You win!");
    		}
    		else {
    			if (numSticks > 1) {
    				System.out.println("There are " + numSticks + " sticks");
    			}
    			else {
    				System.out.println("There is 1 stick");
    			}
    			System.out.println("How many sticks to take? (1 or 2)");
    			numToTake = take.nextInt();
    			if (numToTake<1) {
    				numToTake=1;
    			}
    			else if (numToTake>2) {
    				numToTake=2;
    			}
    			numSticks = numSticks - numToTake;
    			if (numSticks<=0) {
    				System.out.println("You lose!");
    			}
    			
    		}
    	}
    }
    reply.close();
    input.close();
    take.close();
    }
    else {
    	System.out.println("Ok, goodbye!");
    }
    }
    }
  12. #7
  13. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    why the scanners are closed?
    Can you explain what the problem was?
    able to close them
    Can you explain what the problem was?

    If you got errors, copy the full text of the error message and paste it here.


    Also the posted code is not properly formatted. Nested statements (inside {}s ) should be indented 3-4 spaces to show the logic. There should not be }s in the same column together like at the end of the code.
    Last edited by NormR; March 2nd, 2013 at 03:55 PM.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    5
    Rep Power
    0

    Smile


    Originally Posted by NormR
    Can you explain what the problem was?

    Can you explain what the problem was?

    If you got errors, copy the full text of the error message and paste it here.


    Also the posted code is not properly formatted. Nested statements (inside {}s ) should be indented 3-4 spaces to show the logic. There should not be }s in the same column together like at the end of the code.
    There is no problem NormR, I posted my question because I want to be able to understand everything that's happening. I want to be able to understand why it works which to me is just as important as understanding why something doesn't work.

    So even though the scanners are closed, I don't understand why they are closed and therefore I'm asking someone to explain as per my question.

    however, there is something I don't understand and I will post this as a separate reply as I don't want to move on until I fully understand everything that's happening with my program.
    I'm aware the program is not formatted correctly but given that I have been programming a couple of weeks and had to re-do this code a lot to get it to work, some formatting issues are to be expected but the program itself works fine. Now I only want to know why the scanners are closed despite being closed in a different bracket when this didn't work in other programs.

    Do you know why? Are you able to offer some guidance on closing scanners? If not, please do not reply to my post. If you do and are willing to share this information, I'd be grateful for the help.

    Thank you,
  16. #9
  17. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    In general it is good to close resources that a program uses to trim down system requirements, to make sure that usage of the resource is properly finished and to insure that the resource is made available for other parts of a program that could need it.

IMN logo majestic logo threadwatch logo seochat tools logo