【发布时间】:2015-11-04 04:44:35
【问题描述】:
我是第一次学习 Java,在创建一个从标准 52 张卡片组中随机选择两张卡片的程序时遇到了一个小问题。我正在尝试创建一个语句来比较两张牌是否具有相同的等级或相同的花色。当我尝试创建一个 if 语句来执行此操作时,我收到一条错误消息,指出我无法比较两个相同的表达式。我想知道是否有人可以给我一些关于该怎么做的见解。这是我的代码:
package Assignment3;
public class HouseOfCards {
public static void main(String[] args) {
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
for( int i = 0; i < deck.length; i++) deck[i] = i;
for( int i = 0; i < deck.length; i++) {
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
for( int i = 0; i < 2; i++) {
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println(rank + " of " + suit);
}
}
}
我的尝试是:
if (suit==suit)
System.out.println("Cards share the same suit");
else
System.out.println("Cards do not share the same suit");
if (rank==rank)
System.out.println("Cards share the same rank");
else
System.out.println("Card do not share the same rank");
【问题讨论】:
-
你能加入你的尝试吗?
-
if (suit==suit) System.out.println("两张牌同花色"); else System.out.println("纸牌不同花色"); if (rank==rank) System.out.println("卡牌同等级"); else System.out.println("卡片不同等级");
标签: java