Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 2nd, 2013, 07:33 AM
3773 3773 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 5 3773 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 48 sec
Reputation Power: 0
Lightbulb Homework - 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

Reply With Quote
  #2  
Old March 2nd, 2013, 09:56 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
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.

Reply With Quote
  #3  
Old March 2nd, 2013, 11:46 AM
3773 3773 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 5 3773 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 48 sec
Reputation Power: 0
Thumbs up

Quote:
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

Reply With Quote
  #4  
Old March 2nd, 2013, 12:30 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
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"?

Reply With Quote
  #5  
Old March 2nd, 2013, 02:08 PM
3773 3773 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 5 3773 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 48 sec
Reputation Power: 0
Thumbs up

Quote:
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,

Reply With Quote
  #6  
Old March 2nd, 2013, 02:24 PM
3773 3773 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 5 3773 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 48 sec
Reputation 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!");
}
}
}

Reply With Quote
  #7  
Old March 2nd, 2013, 02:52 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
Quote:
why the scanners are closed?

Can you explain what the problem was?
Quote:
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 02:55 PM.

Reply With Quote
  #8  
Old March 5th, 2013, 10:07 AM
3773 3773 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2013
Posts: 5 3773 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 48 sec
Reputation Power: 0
Smile

Quote:
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.

Quote:
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,

Reply With Quote
  #9  
Old March 5th, 2013, 10:39 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,955 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 53 m 34 sec
Reputation Power: 345
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Homework - 21 Sticks...help with code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap