-
Do while question
hi guys
i am new in this forum :)
i have a simple question i think
so, i have this code
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
-
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!
-
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!
-
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:
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 :)
-
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.
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 ";".
-
i have this code
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
-
Redesign your main class. Move some of the code out to methods for better handling.
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);
}
-
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 :)
-
-
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...
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
-
i don't know how i can't modify my code to do what you propose :/
this is where i sum the cards
Code:
totalActualDealer+=(baralho [cartaActual].toIntt());
return (baralho [cartaActual++]);
-
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?
-
thanks for your patience
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
Code:
/*/if (totalValor() >= 12 )
{
valores[12] = 1;}
else
valores[12] = 11; /*/
-
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?
-
it is correct because i have class card with this
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?