在一个Java程序中,我试图返回一副牌中价值最大的一张牌。
钻石是价值最低的西装。然后是梅花,然后是红桃,最后是黑桃,价值最大。正如您所看到的,我有一段代码,在findLargest方法中简单地说是最大的。我不确定这种方法的其余部分应该放在哪里。
public class Card {
private int number;
private String suit;
/*
* Randomly creates a card numbered 1 to 13 (ace = 1!) and labelled "Hearts","Clubs","Diamonds" or "Spades".
*/
public Card() {
double randomNum = Math.random() * 4.0;
if (randomNum < 1.0)
suit = "Hearts";
else if (randomNum < 2.0)
suit = "Clubs";
else if (randomNum < 3.0)
suit = "Diamonds";
else
suit = "Spades";
randomNum = Math.random() * 13.0;
number = (int) randomNum + 1;
}
/*
* Creates a card with specified number and suit
*/
public Card (int n, String s) {
number = n;
suit = s;
}
public int getNumber() {
return number;
}
public String getSuit () {
return suit;
}
public String cardString() {
// System.out.println(number + " " + suit);
String stringNum = "";
switch (number) {
case 1:
stringNum = "Ace";
break;
case 2:
stringNum = "Two";
break;
case 3:
stringNum = "Three";
break;
case 4:
stringNum = "Four";
break;
case 5:
stringNum = "Five";
break;
case 6:
stringNum = "Six";
break;
case 7:
stringNum = "Seven";
break;
case 8:
stringNum = "Eight";
break;
case 9:
stringNum = "Nine";
break;
case 10:
stringNum = "Ten";
break;
case 11:
stringNum = "Jack";
break;
case 12:
stringNum = "Queen";
break;
case 13:
stringNum = "King";
break;
default:
System.out.println("Error in Card - illegal number");
}
return stringNum + " of " + suit;
}
}
public class PackCards {
private ArrayList<Card> pack;
/*
* Create a random pack of size n
*/
public PackCards(int n) {
Card c;
pack = new ArrayList<Card>();
for (int i = 1; i <= n; i++) {
c = new Card();
pack.add(c);
}
}
public void printPack() {
for (Card c : pack) {
System.out.println(c.cardString());
}
}
public Card findLargest() {
if ( c.getNumber() > largest.getNumber() )
largest =
else if (c.getNumber() == largest.getNumber() ) {
if (largest.getSuit().equals("Diamonds"))
largest = largest.getNumber;
}
else if (c.getNumber() == largest.getNumber() ) {
if (largest.getSuit().equals("Clubs"))
largest = largest.getNumber;
}
else if (c.getNumber() == largest.getNumber() ) {
if (largest.getSuit().equals("Hearts"))
largest = largest.getNumber;
}
else return;
}
对于这类工作,您希望利用Java的面向对象特性,并将枚举用于脸值和套装。枚举按您列出它们的顺序“自然排序”,使构造和字符串输出更加容易:
public class Card implements Comparable<Card> {
private FaceValue faceValue;
private Suit suit;
/*
* Randomly creates a card numbered 1 to 13 (ace = 1!)
* labelled "Hearts","Clubs","Diamonds" or "Spades".
*/
public Card() {
int randomSuit = (int)Math.floor(Math.random() * 4.0); //Number between 0 and 3, inclusive.
int randomFace = (int)Math.floor(Math.random() * 13.0);
this.faceValue = FaceValue.values()[randomFace];
this.suit = Suit.values()[randomSuit];
}
public Card(FaceValue value, Suit suit) {
this.faceValue = value;
this.suit = suit;
}
// TODO Constructor that convert ints and strings to proper enums and call previous constructor
public int compareTo(Card that) {
int comparison = this.suit.compareTo(that.suit);
return (comparison != 0) ? comparison : this.faceValue.compareTo(that.faceValue);
}
public String cardString() {
return this.faceValue + " of " + this.suit;
}
public enum Suit{
DIAMOND, CLUB, HEART, SPADE;
}
public enum FaceValue {
ACE, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING;
} // You can modify this enum if you need to give back something more specific as the string value.
}
然后,对于你的卡片组,你也可以通过选择一个好的数据结构来简化事情,在这种情况下,树集将自动使用你为卡片提供的比较功能来对卡片进行排序
public class Deck {
//By using a Set we eliminate duplicates
private final TreeSet<Card> cards;
public Deck(int size) {
cards = Sets.newTreeSet(IntStream.range(0, size)
.mapToObj(n -> new Card())
.collect(Collectors.toList());
}
//Because this is a TreeSet, finding the largest value is trivial.
public Card findLargest() {
return this.cards.last();
}
}
如果您需要为套牌中的牌设置特殊字符串,您将对FaceValue
进行以下修改:
public enum FaceValue {
ACE {
public String toString() { return "Ace"; }
},
ONE {
public String toString() { return "One"; }
},
... // Fill out the rest in the same manner.
KING {
public String toString() { return "King"; }
}
}
请记住,在面向对象编程中,首要规则是对象应该知道其行为。因此,如果您发现自己正在编写switch语句,那么您很可能会将行为推迟到对象本身。在这种情况下,您需要创建分配给给定卡的子对象(面值和套装枚举)。卡片询问这些子部件如何订购,以及它们应该如何响应字符串请求。
您可以更改Card类以实现可比较的接口。例如:
public class Card implements Comparable<Card> {
....// your current implementation for the Card class
public int compareTo(Card another) {
if (this.suit.compareTo(another.suit) == 0) {
if (this.number < another.number) {
return -1;
} else if (this.number > another.number) {
return 1;
} else {
return 0;
}
} else if (this.suit.compareTo("Diamonds") == 0) {
// Diamonds being the lowest valued suit ...
return -1;
} else if (this.suit.compareTo("Clubs") == 0) {
// ... then Clubs ...
if (another.suit.compareTo("Diamonds") == 0) {
return 1;
} else {
return -1;
}
} else if (this.suit.compareTo("Hearts") == 0) {
// ... Hearts ...
if (another.suit.compareTo("Spades") == 0) {
return -1;
} else {
return 1;
}
} else {
// ... and largest values, Spades
return 1;
}
}
}
然后,您可以修改PackCard以将卡存储在有序集合中。例如:
pack = new TreeSet<Card>();
这样,包里的牌就被排序了,最后一张是最大的。
如果您想在列表中添加卡片的顺序,您可以通过以下方式获得最大的卡片:
largest = Collections.max(pack);
我会简化代码,以便更容易看到您想要实现的目标。在Java 8中,您可以这样做。
import java.util.Random;
public class Card {
enum Suit {
// must be in increasing order.
Spades, Diamonds, Clubs, Hearts
}
static final Suit[] SUITS = Suit.values();
private final int number;
private final Suit suit;
/*
* Randomly creates a card numbered 1 to 13 (ace = 1!) and labelled "Hearts","Clubs","Diamonds" or "Spades".
*/
public Card() {
Random rand = new Random();
suit = SUITS[rand.nextInt(SUITS.length)];
number = rand.nextInt(13) + 1;
}
public int getNumber() {
return number;
}
public Suit getSuit() {
return suit;
}
static final String[] NAMES = ",Ace,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten,Jack,Queen,King".split(",");
public String cardString() {
assert number > 1 && number < NAMES.length;
return NAMES[number] + " of " + suit;
}
}
这也简化了您的包
public class PackOfCards {
private final List<Card> cards;
public PackOfCards(int size) {
// note duplicates are possible, i.e. ever card could be the same.
cards = IntStream.range(0, size)
.mapToObj(n -> new Card())
.collect(Collectors.toList());
}
public Card findLargest() {
return cards.stream().max(Comparator.comparing(Card::getNumber)
.thenComparing(Card::getSuit)).get();
}
}
问题内容: 给定这样的表,每个监视器的最新校准信息将是什么查询?换句话说,我想找到每个监视器的最大日期值。特定于Oracle的功能适合我的应用程序。 此示例的结果如下所示: 问题答案: 我倾向于使用解析函数 您也可以使用相关的子查询,尽管效率较低
问题内容: 我试图获取数组中的最大值,同时仍保留项目标签。我知道我可以通过运行sort()来做到这一点,但是如果这样做,我只会丢失标签- 这对于我需要的东西毫无意义。这是数组: 有任何想法吗? 问题答案: 不要对数组进行排序以获取最大值。 获得最大值: 获取相应的密钥:
我正在尝试为类编写代码,该类执行以下操作: 询问要使用多少甲板 每次按enter键时,程序都会从卡片组中生成一张未使用的卡片 当没有更多的卡可以交易时,程序会通知用户 程序允许用户再次播放 方法被广泛使用 到目前为止,我的代码如下所示: 当我尝试运行超过1套牌时,问题就出现了,有时会立即失败,而有时运行可能4个交易然后失败。我似乎也无法让它再次运行;每当我输入“是”而不是再次播放时,它就会终止。如
问题内容: 我有看起来像这样的数据: 如何查询此数据以获得每个组最流行(按计数)的颜色。因此结果将如下所示: 问题答案: 顺便说一句,不要命名字段或。它们是关键字,将导致头痛和令人伤心。
我需要在熊猫数据帧中找到本地最大值和最小值,起初看起来这和熊猫找到本地最大值和最小值是一样的问题,但建议的解决方案似乎都不正确。 我想确定局部最小值和最大值,而不是平台值。正确的标识是: iloc 2或3中的最大值(无所谓) iloc 7中的最小值 一个解决方案是开始写循环和如果/其他,但它变得越来越丑陋...我猜可能有更简单的解决方案,使用熊猫,我缺乏一些知识,任何帮助都将不胜感激。 我对Pyt
NowCoder 题目描述 在一个 m*n 的棋盘的每一个格都放有一个礼物,每个礼物都有一定价值(大于 0)。从左上角开始拿礼物,每次向右或向下移动一格,直到右下角结束。给定一个棋盘,求拿到礼物的最大价值。例如,对于如下棋盘 // 1 10 3 8 12 2 9 6 5 7 4 11 3 7 16 5 礼物的最大价值为 1+12