Pages

Thursday, 25 August 2016

Chapter 7 Exercise 29, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*7.29 (Game: pick four cards) Write a program that picks four cards from a deck of 52 cards and computes their sum. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24.



public class ProgrammingEx7_29 {
 public static void main(String[] args) {
 
  int sum = 0, numberOfPick = 0;
  while (sum != 24) {
   sum = pickCard() + pickCard() + pickCard() +pickCard();
   numberOfPick++;
 
  }
 
  System.out.println("Number of picks:" + numberOfPick*4);
 }
 
 public static int pickCard() {
  int card = (int) (Math.random() * 52.0); // pick a card 0-51
  return card;
 }
 
 public static int getRank(int card) {
 
  return card / 4 + 1; // determine the rank 1-13
 
 }
}

No comments :

Post a Comment