English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

im making a balckjack game so how would i do
like "Hit or Stay?"
Then get an answer

i no it would be system.out.println blah blah

but i dont no what do like input.....

2007-12-19 09:18:01 · 3 answers · asked by damaster 2 in Computers & Internet Programming & Design

3 answers

I always use Java's Scanner for keyboard input.
**Important**
must import

import java.util.Scanner;
//sample code

Scanner kb = new Scanner(System.in); //creates a new Scanner object with "System.in" as the parameter
System.out.println("Hit or Stay "); // prompt the user to hit or stay
String response = kb.nextLine(); //get the users first response
while(response.length() > 0) // loop as long as user input is greater than 0 (empty line)
{

if(response.equalsIgnoreCase( "hit") ) // check for "Hit"
{
cardCount += hit(); //method to hit. which uses a method that adds to a cardCount which keeps track of cards and its value
//no need to break; as loop will ask to hit or stay again, after the card is dealt
}//end if
else if(response.equalsIgnoreCase( "stay" ) ) //check for "Stay
{
stay(); //method to stay, no increment in cardCount
break;//leave the loop
}//end else if
else
{
System.out.println("invalid input");
//user did not enter "hit" or "stay"
}//end else

//Reprompt the user
System.out.println("Hit or Stay ");
response = kb.nextLine();
//within a while loop
}//end while

may need tweaking depending on your program. and may need minor spelling / syntax editing
hopefully this helps =]
good luck!

2007-12-19 09:57:04 · answer #1 · answered by agill15806 2 · 0 0

I never liked BufferedReader etc. So I found this one, which helps a LOT:

http://www.faqs.org/docs/javap/source/TextIO.java

Copy the code and place it into it's own class "TextIO". All the methods are static so you should be able to call on the methods by calling the class.method. Read through to find the methods you need. In this case it looks like a char, so you'd want to use char somechar = TextIO.getlnChar();

2007-12-19 09:31:39 · answer #2 · answered by ɹǝsn ooɥɐʎ ǝɯos ʇsnɾ 3 · 0 0

Take a look at this code example for using:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

Is this something along the lines of what you want?

2007-12-19 09:23:23 · answer #3 · answered by Jim Maryland 7 · 0 0

fedest.com, questions and answers