Results 1 to 15 of 15
  1. #1
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Exclamation Do while question

    hi guys

    i am new in this forum :)

    i have a simple question i think

    so, i have this code

    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();
        				
        			}
    and my ideia is doing a loop with a system.out.println ("vvvvv"); when the answer is Y.

    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

  2. #2
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    167
    Rep Power
    4

    Default

    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!

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  4. #4
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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:

    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();
        				
        			}
    thanks :)

  5. #5
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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.

    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"));
    Notice how the while statement does not contain a block({}) after it, but rather a ";".

  6. #6
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    i have this code

    Java 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
    my doubt is how i can repeat the afirmation "Do you wat to continue after these system out" after the end of game

  7. #7
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    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);
         }

  8. #8
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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.

  9. #9
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    no one can help me?

  10. #10
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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. #11
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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++]);

  12. #12
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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?

  13. #13
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    thanks for your patience

    Java 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;
    	}
    
    }
    i am trying change the vector value with this code but noting happen

    Java Code:
    /*/if (totalValor() >= 12 ) 
    		{
    		valores[12] = 1;}
    		
    		else
    	    valores[12] = 11; /*/
    Last edited by felito; 11-10-2010 at 07:32 PM.

  14. #14
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    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?

  15. #15
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    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

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  2. question
    By sunilgamidi in forum Introductions
    Replies: 0
    Last Post: 09-28-2010, 08:23 AM
  3. Question about what this do.
    By Syfer in forum New To Java
    Replies: 1
    Last Post: 07-03-2010, 08:35 AM
  4. help me in dis question
    By krishan in forum New To Java
    Replies: 10
    Last Post: 02-16-2009, 07:04 PM
  5. Need help on this question
    By Deon in forum New To Java
    Replies: 3
    Last Post: 01-27-2008, 03:58 PM

Posting Permissions

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