首页 > 代码库 > HW6.24

HW6.24

技术分享

技术分享

 

 1 public class Solution 2 { 3     public static void main(String[] args) 4     { 5         int count = 0; 6         int color; 7         int number; 8         String colorStr; 9         String numberStr;10         String[] colorSet = {" ", " ", " ", " "};11         while(true)12         {13             color = (int)(Math.random() * 4);14             number = (int)(Math.random() * 13);15 16             colorStr = getColor(color);17             numberStr = getNumber(number);18 19             System.out.println(numberStr + " of " + colorStr);20 21             count++;22 23             if(!contains(colorSet, colorStr))24                 add(colorSet, colorStr);25 26             if(isFull(colorSet))27                 break;28         }29 30         System.out.println("Number of picks: " + count);31     }32 33     public static boolean isFull(String[] array)34     {35         for(String str: array)36             if(str == " ")37                 return false;38         return true;39     }40 41     public static boolean contains(String[] array, String str)42     {43         for(String i: array)44             if(i == str)45                 return true;46         return false;47     }48 49     public static void add(String[] array, String str)50     {51         for(int i = 0; i < array.length; i++)52             if(array[i] == " ")53                 array[i] = str;54     }55 56     public static String getColor(int n)57     {58         if(n == 0)59             return "Spades";60         else if(n == 1)61             return "Clubs";62         else if(n == 2)63             return "Hearts";64         else65             return "Diamonds";66     }67 68     public static String getNumber(int n)69     {70         if(n == 0)71             return "King";72         else if(n == 1)73             return "Ace";74         else if(n == 11)75             return "Jack";76         else if(n == 12)77             return "Queen";78         else79             return "" + n;80     }81 }

 

HW6.24