/* *Copyright (c)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:Cards.java *作 者:单昕昕 *完成日期:2015年9月22日 *版 本 号:v1.0 *问题描述:输出一张牌的大小和花色。 *程序输入:无。 *程序输出:一张牌的大小和花色。 */ import java.util.ArrayList; import java.util.List; import java.util.Random; public class Test { public enum Rank { // 枚举 Ace("Ace","Ace"), two("2","two"), three("3","three"), four("4","four"), five("5","five"), six("6","six"), seven ("7","seven"), eight("8","eight"), nine("9","nine"), ten("10","ten"), Jack("Jack","Jack"), Queen("Queen","Queen"), King ("King","King"); private String code; private String description; Rank(String code, String description) { this.code = code; this.description = description; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public static ArrayList<Rank> list() { ArrayList<Rank> list= new ArrayList<Rank>(); list.add(Ace); list.add(two); list.add(three); list.add(four); list.add(five); list.add(six); list.add(seven); list.add(eight); list.add(nine); list.add(ten); list.add(Jack); list.add(Queen); list.add(King); return list; } } public enum Suit { // 枚举 Clubs("Clubs","Club"), Diamonds("Diamonds","Diamond"), Hearts("Hearts","Heart"), Spades("Spades","Spade"); private String code; private String description; Suit(String code, String description) { this.code = code; this.description = description; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public static ArrayList<Suit> list2() { ArrayList<Suit> list2= new ArrayList<Suit>(); list2.add(Clubs); list2.add(Diamonds); list2.add(Hearts); list2.add(Spades); return list2; } } public static void main(String[] args) { ArrayList<Rank>list=Rank.list(); Random random1= new Random(); int num1=random1.nextInt(list.size())+1; System.out.print(list.get(num1).getCode()); System.out.print(" "); ArrayList<Suit>list2=Suit.list2(); Random random2= new Random(); int num2=random2.nextInt(list2.size())+1; System.out.println(list2.get(num2).getCode()); } }
贴上另外一种:
import java.util.*; public class Test { public static void main(String[] args) { String []suits={"Spades","Hearts","Diamonds","Clubs"}; String []ranks={"Ace","2","3","4","5","6","7","8","9","10","Jack","Queen","King"}; Random random = new Random(); int t1=Math.abs(random.nextInt())%13; int t2=Math.abs(random.nextInt())%4; System.out.print("The card you picked is "); System.out.print(suits[t1]); System.out.print(" of "); System.out.println(ranks[t2]); } }
学习心得:
感觉比较麻烦,我先从网上找的相关代码修改成为己用的~
学长教了我另外一种比较简单的方法,我要试试~~