-1

I have made this code and everything works fine but I would like the user to press enter to continue rolling until they either win or lose. Any suggestions would be great.

import hsa.Console;
import java.util.Scanner;  // Needed for the Scanner class
import java.util.Random;

public class Craps_Final1
{
     static Console c;
     public static void main(String [] args)   
     {
          c = new Console ();

          int point; // Dices Rolled 
          int sum; // Sum of the Dices 
          char input; // Menu Selector
          char menu; // return menu
          String player;// Name of player
          int bank;// money holder for the player
          int bet; // bet per per round
          String player2;// Name of player
          int bank2;// money holder for the player
          int bet2; // bet per per round
          int game; // input for 1 player or 2 player
          int loop=1; // loop for game



          while(true)
          {

               input = menu(); 
               if(input == 'M' || input == 'm')
               {

                    while(true)
                    {
                         menu = returnMenu(); 
                         if(menu == 'Q' || menu == 'q')
                         {
                              c.close();
                              break;
                         }

                         if(menu == 'P' || menu == 'p')
                         {
                              c.clear();

                    while(true)
                    {

                         Random randomNumbers = new Random();

                         Scanner keyboard = new Scanner(System.in);
                         c.println ("would you like to play 1 player or 2 player");
                         game=c.readInt();
                         c.clear();

                         if(game==1)
                         {

                         //PLAYER 1
                         c.println("Please enter your name");
                         player=c.readString();
                         c.clear();
                         c.println("Please enter how much you would like to gamble with today?");
                         bank=c.readInt();
                         c.clear();
                         c.println("\n");
                         c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + bank);
                         c.println("How much would you like to Bet?");
                         bet=c.readInt();
                         c.clear();
                         c.println("\n");
                         c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                         c.println("Bet:" + bet);
                         c.println("Available:" + (bank));                       

                         c.println("First roll.\n");
                         sum = roleDice(randomNumbers);
                         c.println("Sum:\t" + sum + "\n");

                         switch (sum) 
                         {

                              case 7: case 11:
                                   c.println(player + " " + "wins.");
                                   bank=(bet*2)+bank;
                                   c.println("Your Win has been added to your available balance");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   c.setCursor(23,4);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                                   break;

                              case 2: case 3: case 12:
                                   c.println(player + " " + "loses.");
                                   c.println("Your Loss has been deducted from your available balance");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   bank=bank-bet;
                                   c.setCursor(23,4);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                                   break;

                              case 4: case 5: case 6:  case 8: case 9: case 10:
                                   c.println(player + " " + "Your Bet Stays and your available balance has not changed");
                                   c.setCursor(25,4);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                                   break;                              
                         }                              
                         }
                         else
                         {
                               //PLAYER 1 and PLAYER 2
                         c.println("Player 1 enter your name");
                         player=c.readString();
                         c.clear();
                         c.println("Player 2 enter your name");
                         player2=c.readString();
                         c.clear();
                         c.println("Player 1 how much you would like to gamble with today ?");
                         bank=c.readInt();
                         c.clear();
                         c.println("Player 2 how much you would like to gamble with today ?");
                         bank2=c.readInt();
                         c.clear();
                         c.println("\n");
                         c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + bank);
                         c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + bank2);
                         c.println("Player 1 how much would you like to Bet ?");
                         bet=c.readInt();
                         c.println("Player 2 how much would you like to Bet ?");
                         bet2=c.readInt();
                         c.clear();
                         c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                         c.println("Bet:" + bet);
                         c.println("Available:" + (bank));
                         c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                         c.println("Bet:" + bet2);
                         c.println("Available:" + (bank2));
                         c.clear();
                         c.println("First roll.\n");
                         sum = roleDice(randomNumbers);
                         c.println("Sum:\t" + sum + "\n");

                         switch (sum) 
                         {

                              case 7: case 11:
                                   c.println("All players wins.");
                                   bank=(bet*2)+bank;
                                   bank2=(bet2*2)+bank2;
                                   c.println("Your Win has been added to your available balance");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   c.setCursor(21,1);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + (bank));
                                   c.setCursor(22,1);
                                   c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                                   break;

                              case 2: case 3: case 12:
                                   c.println("All players lose.");
                                   bank=bank-bet;
                                   bank2=bank2-bet2;
                                   c.println("Your Loss has been deducted from your available balance");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   c.setCursor(21,1);
                                   c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + (bank));
                                   c.setCursor(22,1);
                                   c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                                   break;

                              case 4: case 5: case 6:  case 8: case 9: case 10:
                                   c.println("All Bets Stays and your available balances have not changed");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   c.setCursor(21,1);
                                   c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + (bank));
                                   c.setCursor(22,1);
                                   c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                                   break;       

                         }
                         }
                        break;
                    }                       
                              break;  
                         }

                    }    
                    break;
               }
               else if (input == 'P' || input == 'p')
               {
                    c.clear();
                    while(true)
                    {

                         Random randomNumbers = new Random();

                         Scanner keyboard = new Scanner(System.in);
                         c.println ("would you like to play 1 player or 2 player");
                         game=c.readInt();
                         c.clear();

                         if(game==1)
                         {
                         //PLAYER 1
                         c.println("Please enter your name");
                         player=c.readString();
                         c.clear();
                         c.println("Please enter how much you would like to gamble with today?");
                         bank=c.readInt();
                         c.clear();
                         c.println("\n");
                         c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + bank);
                         c.println("How much would you like to Bet?");
                         bet=c.readInt();
                         c.clear();
                         c.println("\n");
                         c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                         c.println("Bet:" + bet);
                         c.println("Available:" + (bank));                       

                         c.println("First roll.\n");
                         sum = roleDice(randomNumbers);
                         c.println("Sum:\t" + sum + "\n");

                         switch (sum) 
                         {

                              case 7: case 11:
                                   c.println(player + " " + "wins.");
                                   bank=(bet*2)+bank;
                                   c.println("Your Win has been added to your available balance");
                                   c.setCursor(25,4);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                                   break;

                              case 2: case 3: case 12:
                                   c.println(player + " " + "loses.");
                                   c.println("Your Loss has been deducted from your available balance");
                                   bank=bank-bet;
                                   c.setCursor(25,4);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                                   break;

                              case 4: case 5: case 6:  case 8: case 9: case 10:
                                   c.println(player + " " + "Your Bet Stays and your available balance has not changed");
                                   c.setCursor(25,4);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                                   break;                              
                         }      
                         }
                         else
                         {
                               //PLAYER 1 and PLAYER 2
                         c.println("Player 1 enter your name");
                         player=c.readString();
                         c.clear();
                         c.println("Player 2 enter your name");
                         player2=c.readString();
                         c.clear();
                         c.println("Player 1 how much you would like to gamble with today ?");
                         bank=c.readInt();
                         c.clear();
                         c.println("Player 2 how much you would like to gamble with today ?");
                         bank2=c.readInt();
                         c.clear();
                         c.println("\n");
                         c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + bank);
                         c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + bank2);
                         c.println("Player 1 how much would you like to Bet ?");
                         bet=c.readInt();
                         c.println("Player 2 how much would you like to Bet ?");
                         bet2=c.readInt();
                         c.clear();
                         c.println("\t\t\t" + "Player:" + player + "\t" + "Available:" + (bank));
                         c.println("Bet:" + bet);
                         c.println("Available:" + (bank));
                         c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                         c.println("Bet:" + bet2);
                         c.println("Available:" + (bank2));
                         c.clear();
                         c.println("First roll.\n");
                         sum = roleDice(randomNumbers);
                         c.println("Sum:\t" + sum + "\n");

                         switch (sum) 
                         {

                              case 7: case 11:
                                   c.println("All players wins.");
                                   bank=(bet*2)+bank;
                                   bank2=(bet2*2)+bank2;
                                   c.println("Your Win has been added to your available balance");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   c.setCursor(21,1);
                                   c.println("\n");
                                   c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + (bank));
                                   c.setCursor(22,1);
                                   c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                                   break;

                              case 2: case 3: case 12:
                                   c.println("All players lose.");
                                   bank=bank-bet;
                                   bank2=bank2-bet2;
                                   c.println("Your Loss has been deducted from your available balance");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   c.setCursor(21,1);
                                   c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + (bank));
                                   c.setCursor(22,1);
                                   c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                                   break;

                              case 4: case 5: case 6:  case 8: case 9: case 10:
                                   c.println("All Bets Stays and your available balances have not changed");
                                   c.println ("New Balances shown at the bottom of the Screen");
                                   c.setCursor(21,1);
                                   c.println("\t\t\t" + "Player1:" + player + "\t" + "Available:" + (bank));
                                   c.setCursor(22,1);
                                   c.println("\t\t\t" + "Player2:" + player2 + "\t" + "Available:" + (bank2));
                                   break;       

                         }
                         }
                        break;
                    }
                    break;
               }

               else if(input == 'Q' || input == 'q')
               {
                    c.close();
                    break;
               }   
          }

     }
     // Sub Methods 

     // Random Number Generater 
     public static int roleDice(Random randomNumbers) 
     {
          int dice1, dice2;

          dice1 = 1+randomNumbers.nextInt(5);
          dice2 = 1+randomNumbers.nextInt(5);

          c.println("Dice 1:\t" + ++dice1);
          c.println("Dice 2:\t" + ++dice2);

          return dice1 + dice2;

     }

     // Menu Slector 
     public static char menu ()
     {
          char letter;
          c.clear();
          c.println("\t\t" + "        " + "*-----------------------------*");
          c.println ("\t\t" + "    " + "**********" + "Game Slection Menu" +"***********" );
          c.println("\t\t" + "        " +"*-----------------------------*");
          c.println ("\n\n");
          c.println("\t\t" +"M - For User Manual ");
          c.println("\t\t" +"P - Play Game");
          c.println("\t\t" +"Q - To Quit");
          letter = c.readChar();
          c.readChar();

          return letter;
     }

     // Return Menu
     public static char returnMenu ()
     {
          char letter2;
          c.clear();
          c.println ("\t\t\t" + "*******" + "User Manual:" + "*******" );
          c.println ("\n");
          c.println("        **********************************************************************");
          c.println("        *                                                                    *");
          c.println ("\t"+ "* This game is simple to play.                                       *");
          c.println ("\t"+ "* 2 Dices will be rolled and the numbers will be added together      *");
          c.println ("\t"+ "* If the sum of numbers rolled is equal to 7 or 11 its a Win         *");
          c.println ("\t"+ "* If the sum of numbers rolled is equal to 2,3 or 12 its a Loss      *");
          c.println ("\t"+ "* If the sum of numbers rolled is equal to 4,5,6,8,9,10   Bet remains*");
          c.println("        *                                                                    *");
          c.println("        **********************************************************************");

          c.println("\t\t" +"P - To Play Game");
          c.println("\t\t" +"Q - To Quit");
          letter2 = c.readChar();
          return letter2;
     }


}
4

2 回答 2

2

In java, you should use the Scanner. You can see how to use it here How can I read input from the console using the Scanner class in Java?

于 2013-06-09T08:43:34.673 回答
0

我想说的是,与其将代码嵌入 main 中,不如使用不同的方法,在该方法结束时,您询问用户是否想再次播放,如果是,则如果程序没有终止,则该方法再次递归调用自身,所以然后你在 main 例如调用它

    public static void play()
    {
      //place code here

     S.o.p("play again?");
    //if yes call itself to startover
     play();
    //if no, program terminates
    }

然后在 main 你只有一个方法调用

    public static void main(String...args)
    {
      play();
    }

这是我的建议。

编辑:为了避免堆栈溢出,我提供了一个替代方案

    public static void play()
    {
      //place code here
    }

    public boolean playAgain(int choice)
    {
      if(!choice==1)
       return false;

     Return true;
    }

    public staic void main(String...args)
    {
     int repeat =0;
      do
       {
         play();
        S.o.p("play again?\n enter 1 for yes,0 for no");
        //assign repeat the number choice
       }while(playAgain(repeat));
    }

注意:代码假定用户输入正确。

于 2013-06-09T09:47:11.353 回答