• وبلاگ : ورودي هاي 88 كامپيوتر دانشگاه شهيد چمران اهواز
  • يادداشت : کلاس حل تمرين جاوا
  • نظرات : 0 خصوصي ، 24 عمومي
  • mp3 player شوکر

    نام:
    ايميل:
    سايت:
       
    متن پيام :
    حداکثر 2000 حرف
    كد امنيتي:
      
      
     
    + ريحانه 
    public class Craps
    6 {
    7 // create random number generator for use in method rollDice
    8 private Random randomNumbers = new Random();
    9
    10 // enumeration with constants that represent the game status
    11 private enum Status { CONTINUE, WON, LOST };
    12
    13 // constants that represent common rolls of the dice
    14 private final static int SNAKE_EYES = 2;
    15 private final static int TREY = 3;
    16 private final static int SEVEN = 7;
    17 private final static int YO_LEVEN = 11;
    18 private final static int BOX_CARS = 12;
    19
    20 // plays one game of craps
    21 public void play()
    22 {
    23 int myPoint = 0; // point if no win or loss on first roll
    24 Status gameStatus; // can contain CONTINUE, WON or LOST
    25
    26 int sumOfDice = rollDice(); // first roll of the dice
    27
    28 // determine game status and point based on first roll
    29 switch ( sumOfDice )
    30 {
    31 case SEVEN: // win with 7 on first roll
    32 case YO_LEVEN: // win with 11 on first roll
    33 gameStatus = Status.WON;
    34 break;
    35 case SNAKE_EYES: // lose with 2 on first roll
    36 case TREY: // lose with 3 on first roll
    37 case BOX_CARS: // lose with 12 on first roll
    38 gameStatus = Status.LOST;
    39 break;
    40 default: // did not win or lose, so remember point
    41 gameStatus = Status.CONTINUE; // game is not over
    42 myPoint = sumOfDice; // remember the point
    43 System.out.printf( "Point is %d\n", myPoint );
    44 break; // optional at end of switch
    45 } // end switch
    46
    47 // while game is not complete
    48 while ( gameStatus == Status.CONTINUE ) // not WON or LOST
    49 {
    50 sumOfDice = rollDice(); // roll dice again
    51
    52 // determine game status
    53 if ( sumOfDice == myPoint ) // win by making point
    54 gameStatus = Status.WON;
    55 else
    56 if ( sumOfDice == SEVEN ) // lose by rolling 7 before point
    57 gameStatus = Status.LOST;
    58 } // end while
    59
    60 // won or lost message
    61 if ( gameStatus == Status.WON )
    62 System.out.println( "Player wins" );
    63 else
    64 System.out.println( "Player loses" );
    65 } // end method play
    66
    67 // roll dice, calculate sum and results
    68 public int rollDice()
    69 {
    70 // pick random die values
    71 int die1 = 1 + randomNumbers.nextInt( 6 ); // first die roll
    72 int die2 = 1 + randomNumbers.nextInt( 6 ); // second die roll
    73
    74 int sum = die1 + die2; // sum of die values
    75
    76 // results of this roll
    77 System.out.printf( "Player rolled %d + %d = %d\n",
    78 die1, die2, sum );
    79
    80 return sum; // return sum of dice
    81 } // end method rollDice
    82 } // end class Craps





    Figure 6.10. Application to test class Craps.
    (This item is ed on page 253 in the print version)
    1 // Fig. 6.10: CrapsTest.java
    2 // Application to test class Craps.
    3
    4 public class CrapsTest
    5 {
    6 public static void main( String args[] )
    7 {
    8 Craps game = new Craps();
    9 game.play(); // play one game of craps
    10 } // end main
    11 } // end class CrapsTest

    Player rolled 5 + 6 = 11
    Player wins
    Player rolled 1 + 2 = 3
    Player loses
    Player rolled 5 + 4 = 9
    Point is 9
    Player rolled 2 + 2 = 4
    Player rolled 2 + 6 = 8
    Player rolled 4 + 2 = 6
    Player rolled 3 + 6 = 9
    Player wins
    Player rolled 2 + 6 = 8
    Point is 8
    Player rolled 5 + 1 = 6
    Player rolled 2 + 1 = 3
    Player rolled 1 + 6 = 7
    Player loses