gpt4 book ai didi

java - 编写 CardTrick 程序,我无法确定用户的卡是否是从一副牌中随机抽取的七张卡中的一张

转载 作者:行者123 更新时间:2023-12-01 16:22:06 27 4
gpt4 key购买 nike

有关代码的所有内容都工作正常,直到我在 Cardtrick 类中添加最后一个 If 语句以查看用户卡片选择是否与七张随机卡片匹配。当我不运行 If 语句时,代码按我的预期工作,我看到 7 张随机卡。我添加了 Card 类,以防万一你们想看它。

`package pickacard;

import java.util.Scanner;

public class CardTrick {

public static void main(String[] args){
Scanner input = new Scanner(System.in);
Card[] magicHand = new Card[7];

for (int i=0; i<magicHand.length; i++){

Card c = new Card();

c.setValue(c.randomValue());
c.setSuit(Card.SUITS[c.randomSuit()]);
magicHand[i] =c;
}

for (Card magicHand1 : magicHand) {
System.out.println(magicHand1.getSuit() + " " + magicHand1.getValue());
}
System.out.println("Pick a card, any card");
System.out.println("Pick the card value");

int UserValue = input.nextInt();
System.out.println("Pick the card suit");
System.out.println("1 = Hearts, 2 = Diamonds, 3 = Spades, 4 = Clubs");

int UserSuit = input.nextInt();

System.out.println("Your card is: " + UserValue + " of " + UserSuit);
System.out.println("Let's see if your card is in the magic hand");

if(UserValue== magicHand1.getValue() && UserSuit == magicHand1()){
System.out.println("Great job");
}
}
}





package pickacard;

/**
* A class that models playing card Objects. Cards have
* a value (note that Ace = 1, Jack -11, Queen =12, King = 13)
* A suit (clubs, hearts, spades, diamonds).
* There are 52 cards in a deck, no jokers
*/
public class Card {

private String suit; //clubs, spades, diamonds, hearts
private int value;//1-13

public static final String [] SUITS = {"Hearts", "Diamonds", "Spades", "Clubs"};
/**
* @return the suit
*/
public String getSuit() {
return suit;
}

/**
* @param suit the suit to set
*/
public void setSuit(String suit) {
this.suit = suit;
}

/**
* @return the value
*/
public int getValue() {
return value;
}

/**
* @param value the value to set
*/
public void setValue(int value) {
this.value = value;
}

public int randomSuit()
{
int value= (int)(Math.random()*4)+0;
return value;
}
public int randomValue()
{
int value= (int)(Math.random()*13)+1;
return value;
}
}

最佳答案

&& UserSuit == magicHand1()

这是将 intCard 进行比较。您可能想检查 magicHand1.getSuit() 的值,但您需要确保用户输入已转换为您用于 suit< 的字符串之一.

关于java - 编写 CardTrick 程序,我无法确定用户的卡是否是从一副牌中随机抽取的七张卡中的一张,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62242131/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com