Results 1 to 15 of 15
Thread: Do while question
- 11-09-2010, 04:33 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
Do while question
hi guys
i am new in this forum :)
i have a simple question i think
so, i have this code
and my ideia is doing a loop with a system.out.println ("vvvvv"); when the answer is Y.Java Code:String input; Scanner s = new Scanner(System.in); do { System.out.print("Would you like to continue (yes or no): "); input = s.nextLine(); while(!input.equals("no") && !input.equals("yes")) { System.out.println("Error: Invalid input!"); System.out.print("Enter yes or no: "); input = s.nextLine(); }
any ideia? is two loops, one to invalid key error or N and another when key Y is pressed- doing system.out - and at the end doing new massage Would you like continue?.
thanks
- 11-09-2010, 05:24 AM #2
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Hello felito,
OKay, so first I'd recommend that you import the following classes:
import java.io.*;
import java.util.Scanner;
That will help you handle the answers from the user.
The second thing is that you're going to want to define a main class, the ubiquitous:
[code]
public class NameOfYourClass {
public static void main (String[] args) throws IOException
{
Your code here
}
}
[/code]
I'd start by doing that!
- 11-09-2010, 06:33 AM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Usually, for programs that can repeat themselves, you stick the actual program code into one method, and then the main method handles the looping. Beyond that, I'd have trouble offering any advice, as you haven't really given us a detailed explanation of what you want, what you've tried, and what you're having trouble with.
Also, welcome to the forum!If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 11-09-2010, 03:50 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
hi guys
thanks for reply :)
I am doing the blackjack game, and after the game i want the question "would you like to continue?" If the user press y key the program repeat the game, else N or other key program doing the same question "would you like continue?"
I have the class main, and the basic of he game now.
so, i have this code for loop question and below of this code i have the script game:
thanks :)Java Code:String input; Scanner s = new Scanner(System.in); do { System.out.print("Would you like to continue (yes or no): "); input = s.nextLine(); while(!input.equals("no") && !input.equals("yes")) { System.out.println("Error: Invalid input!"); System.out.print("Enter yes or no: "); input = s.nextLine(); }
- 11-09-2010, 04:33 PM #5
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Check your syntax for you're do-while loop.
The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
But basically you should have everything you want to repeat and do inside the do block{}.
There is no block after the while.
Notice how the while statement does not contain a block({}) after it, but rather a ";".Java Code:String input; Scanner s = new Scanner(System.in); do { System.out.println("Would you like to continue (yes or no): "); input = s.nextLine(); } while (!input.equals("no") && !input.equals("yes"));
- 11-09-2010, 04:40 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
i have this code
my doubt is how i can repeat the afirmation "Do you wat to continue after these system out" after the end of gameJava Code:package pacote.blackjack; import java.util.Scanner; //Fig. 7.11: DeckOfCardsTest.java // Card shuffling and dealing application. public class Blackjack { private static Scanner input; // execute application public static void main( String args[] ) { CardDeck myDeckOfCards = new CardDeck(); myDeckOfCards.shuffle(); // place Cards in random order System.out.println("BlackJack Game");System.out.println(); String input; Scanner s = new Scanner(System.in); do { System.out.print("Would you like to continue (yes or no): "); input = s.nextLine(); while(!input.equals("no") && !input.equals("yes")) { System.out.println("Error: Invalid input!"); System.out.print("Enter yes or no: "); input = s.nextLine(); } } while(input.equals("no")); System.out.println("The dealer have this card : " + myDeckOfCards.darCartaDealer()); System.out.println("Total value of cards " + myDeckOfCards.totalValorDealer()); System.out.println(); for ( int i = 0; i < 2; i++ ) { System.out.println("You have these cards : " + myDeckOfCards.darCarta()); } System.out.println(); System.out.println("Total value of cards " + myDeckOfCards.totalValor()); } } // end main // end class DeckOfCardsTest
- 11-09-2010, 04:44 PM #7
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Redesign your main class. Move some of the code out to methods for better handling.
Java Code:public static void main(String[] args) { boolean continue = true; do { playGame();//Holds all the code to play a single game of black jack continue = getContinueSelect();//prompts the user to see if he wants to continue playing the game or not. } while (continue); }
- 11-10-2010, 03:14 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
Solved
thanks man
Another question about blackjack
the card ace have the value of 11 or 1. The value depends on the player's action. If is 11 the value of ace is another 11, but if 20 the value of the ace is 1
how i can do this ?
at the moment i have this
public CardDeck()
{
String faces[] = {"dois", "tres", "quatro", "cinco", "seis", "sete", "oito", "nove", "dez", "rei", "dama", "valete", "as"};//faces
int valores [] = {2,3,4,5,6,7,8,9,10,10,10,10,11};//values
String naipes[] = {"copas", "ouros", "paus", "espadas" }; //suits
baralho = new Card [numero_cartas];//52 number_cards deck
for (int count = 0; count < baralho.length; count++){
baralho [count] = new Card (faces [count % 13], naipes [count / 13], valores [count % 13]);
}}
baralho is deck of cards
thanks :)Last edited by felito; 11-10-2010 at 05:01 AM.
- 11-10-2010, 05:23 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
no one can help me?
- 11-10-2010, 05:31 PM #10
Give a thread 24 hours to get a reply before you "bump" it. I can't speak for others, but I don't tend to visit the forum while I'm asleep.
Personally I'd count the baralho array with an aces count. So, each time, you add 1 for an ace, and at the end of the loop, if the count is less than 12, add ten for an ace.
Something like this...
Java Code:int aces = 0 int ccount = 0 for (card in your hand): if (card is an ace): add 1 to aces add card count to ccount while (aces > 0 and ccount < 12): add 10 to ccount
- 11-10-2010, 06:46 PM #11
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
i don't know how i can't modify my code to do what you propose :/
this is where i sum the cards
Java Code:totalActualDealer+=(baralho [cartaActual].toIntt()); return (baralho [cartaActual++]);
- 11-10-2010, 07:25 PM #12
So... you add the value of the first card, then return an incremented pointer?
Can you show more of the method you use? How it's called, what parameters it accepts?
- 11-10-2010, 07:30 PM #13
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
thanks for your patience
i am trying change the vector value with this code but noting happenJava Code:package pacote.blackjack; import java.util.Random; public class CardDeck { private Card baralho[]; private int cartaActual; private final int numero_cartas = 52; private Random aleatorio; private int totalActual; private int totalActualDealer; public CardDeck() { String faces[] = {"dois", "tres", "quatro", "cinco", "seis", "sete", "oito", "nove", "dez", "rei", "dama", "valete", "as"}; int valores [] = {2,3,4,5,6,7,8,9,10,10,10,10,11}; String naipes[] = {"copas", "ouros", "paus", "espadas" }; baralho = new Card [numero_cartas]; /*/if (totalValor() >= 12 ) { valores[12] = 1;} else valores[12] = 11; /*/ for (int count = 0; count < baralho.length; count++){ baralho [count] = new Card (faces [count % 13], naipes [count / 13], valores [count % 13]); } } public void shuffle () { aleatorio = new Random(); cartaActual = 0; totalActual = 0; totalActualDealer = 0; for (int first = 0; first < baralho.length; first++) { int second = aleatorio.nextInt(numero_cartas); Card temp = baralho [first]; baralho [first] = baralho [second]; baralho [second] = temp; } } public Card darCartaDealer() { if (cartaActual< 10) { totalActualDealer+=(baralho [cartaActual].toIntt()); return (baralho [cartaActual++]); } else { return null; } } public int totalValorDealer() { return totalActualDealer; } public Card darCarta() { if(cartaActual < 10) { totalActual+=(baralho [cartaActual].toInt()); return ( baralho [cartaActual++]); } else { return null; } } public int totalValor() { return totalActual; } }
Java Code:/*/if (totalValor() >= 12 ) { valores[12] = 1;} else valores[12] = 11; /*/Last edited by felito; 11-10-2010 at 07:32 PM.
- 11-10-2010, 07:36 PM #14
In fact I don't even think that method compiles; you use .toInt() and .toIntt()... which is correct?
I don't think using this method of summing the cards will get you what you want to achieve. A running tally of which cards are summed is never kept, so you cannot tell which cards are aces.
What you'd be best to do is create a new class, such as CardHand which contains an array of Cards. Then you can have a CardHand object for the dealer and one for the player. In this class you can have a sum method (which uses the logic I showed above) to get the count.
Honestly it doesn't make sense to store the dealer's hand where you store it. His hand has nothing to do with the deck itself.
Do you see what I'm saying? Does it make sense why you would do it this way?
- 11-10-2010, 07:46 PM #15
Member
- Join Date
- Nov 2010
- Posts
- 48
- Rep Power
- 0
it is correct because i have class card with this
Java Code:package pacote.blackjack; public class Card { private String face; private String naipe; private int numeros; public Card (String faceCarta, String faceNaipe, int valor) { face = faceCarta; naipe = faceNaipe; numeros = valor; } public String toString() { return face + " de " + naipe + "\n" + "Value of card: " + numeros; } public int toInt() { return numeros; } public int toIntt() { return numeros; } }
at the moment the application is ok, works fine, but the value of ace is fixed, is eleven and nothing more.
so the problem is exclusively the ace value 11 or 1.
change the array value don't work why? and to do a new class to store the cards what modification i need to do in the cardeck class?Last edited by felito; 11-10-2010 at 07:50 PM.
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
question
By sunilgamidi in forum IntroductionsReplies: 0Last Post: 09-28-2010, 08:23 AM -
Question about what this do.
By Syfer in forum New To JavaReplies: 1Last Post: 07-03-2010, 08:35 AM -
help me in dis question
By krishan in forum New To JavaReplies: 10Last Post: 02-16-2009, 07:04 PM -
Need help on this question
By Deon in forum New To JavaReplies: 3Last Post: 01-27-2008, 03:58 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks