Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Looping a switch/case and adding a variable each time
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Varsteil




PostPosted: Fri Dec 14, 2012 10:37 am   Post subject: Looping a switch/case and adding a variable each time

This is the portion of code that I'm having trouble with
code:
System.out.println("Your starting hand is: " + playerCard1 + " + " + playerCard2);
    playerHand = playerCard1 + playerCard2;
    System.out.println("Press 1 to hit, press 2 to fold, press 3 to stay");
    action = input.nextInt();
    switch (action) {
    case 1: playerCard3 = (cards.nextInt(9+1)); playerHand = playerHand + playerCard1 + playerCard2 + playerCard3; System.out.println("You were dealt: " + playerCard3); break;
    case 2: System.out.println("You lose!"); break;
    case 3: break;

This is my Blackjack game, what I want to do is loop the options and the switch and case. I want to create an i variable, and re-write case 1 as something like
code:
case 1: i++; playerCardi = (cards.nextInt(9+1)); playerHand = playerHand + playerCard1 + playerCard2 + playerCardi; System.out.println("You were dealt: " + playerCardi); break;

In addition, I need a way to add another + playerCardi every time the player chooses to hit. How would I do this? (All the variables have already been declared)
Sponsor
Sponsor
Sponsor
sponsor
TokenHerbz




PostPosted: Fri Dec 14, 2012 4:42 pm   Post subject: Re: Looping a switch/case and adding a variable each time

I'm not sure what you mean looping cases? You would just construct it as any other loop.

If you can give more information to exactly what your attempting to do maybe I could help more Smile

code:

public class CaseLoop {
   
    public static void main(String args[]) {
       
        int loopTimes = 5;
       
        do {
            switch (getInput(loopTimes)) {
                case 0 : System.out.println("Case ZERO is EVEN @ "+loopTimes);
                    break;
                case 1 : System.out.println("Case 1 is ODD! @ "+loopTimes);
                    break;
                default: System.out.println("Defaults are good!");
                    break;
            }
            loopTimes--;
        }while (loopTimes > 0) ;
    }
   
    public static int getInput (int loopLeft) {
            return loopLeft % 2;
        }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: