Java 选择四张卡片并计算它们的总和
我有我的大部分程序,但我已经接近尾声了,我会喜欢的帮助 我必须写一个程序,从52张牌中选出四张牌,然后计算总数。王牌、国王、王后和杰克分别代表1、13、12和11。该程序应显示得出24之和的拾取数 到目前为止,我所拥有的:Java 选择四张卡片并计算它们的总和,java,Java,我有我的大部分程序,但我已经接近尾声了,我会喜欢的帮助 我必须写一个程序,从52张牌中选出四张牌,然后计算总数。王牌、国王、王后和杰克分别代表1、13、12和11。该程序应显示得出24之和的拾取数 到目前为止,我所拥有的: public class Exercise07_29 { public static void main(String[] args){ //initialize everything int[] deck = new int[52
public class Exercise07_29 {
public static void main(String[] args){
//initialize everything
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
//initialize the cards
for(int i = 0; i< deck.length; i ++)
deck[i] = i;
//shuffle the cards
for(int i = 0; i < deck.length; i++){
//generate an index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
//display the four cards
for(int i = 0; i < 4; i++){
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println(rank + " of " + suit);
}
//initialize Ace Jack Queen King
int Ace, Jack, Queen, King;
//Assign a point vale to each
int[] points = {Ace = 1, Jack = 11, Queen = 12, King = 13};
//add the cards together and show output
}
}
public class Exercise07\u 29{
公共静态void main(字符串[]args){
//初始化一切
int[]甲板=新int[52];
字符串[]套装={“黑桃”、“红桃”、“钻石”、“梅花”};
字符串[]排名={“王牌”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“杰克”、“女王”、“国王”};
//初始化卡
对于(int i=0;i
我尝试了一个加法循环,但在将随机输出相加时遇到了问题
我们将非常感谢您的任何帮助!:) 我不知道你这里有什么问题。如果您想知道如何将王牌、杰克、皇后和国王映射到各个点,那么我建议如下
Map<Integer, String> cardToPoints = new HashMap<Integer, String>();
cardToPoints.add(1, "Ace");
cardToPoints.add(11, "Jack");
cardToPoints.add(12, "Queen");
cardToPoints.add(13, "King");
然后从地图上取卡片,得到分数,求和并打印准确的卡片。简单且干净(地图初始化的52个条目除外) Java是一种面向对象的语言。你可能是个初学者,但这里有一个例子供你思考
package cards;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Exercise07_29
* @author Michael
* @link https://stackoverflow.com/questions/31639964/pick-four-cards-and-compute-their-sum-java
* @since 7/26/2015 1:42 PM
*/
public class Exercise07_29 {
public static final int NUM_CARDS = 4;
public static void main(String[] args) {
Deck deck = new Deck();
List<Card> hand = new ArrayList<>();
int score = 0;
for (int i = 0; i < NUM_CARDS; ++i) {
Card card = deck.deal();
hand.add(card);
score += card.getRank().getValue();
}
System.out.println(hand);
System.out.println(score);
}
}
enum SUIT {
CLUB, DIAMOND, HEART, SPADE;
}
enum RANK {
ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);
private final int value;
RANK(int value) { this.value = value; }
public int getValue() {
return value;
}
}
class Card implements Comparable<Card> {
private final SUIT suit;
private final RANK rank;
public Card(SUIT suit, RANK rank) {
if (suit == null) throw new IllegalArgumentException("suit cannot be null");
if (rank == null) throw new IllegalArgumentException("rank cannot be null");
this.suit = suit;
this.rank = rank;
}
public SUIT getSuit() {
return suit;
}
public RANK getRank() {
return rank;
}
@Override
public int compareTo(Card other) {
if (this.getRank().equals(other.getRank())) {
return this.getSuit().compareTo(other.getSuit());
} else {
return this.getRank().getValue() - other.getRank().getValue();
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
return suit == card.suit && rank == card.rank;
}
@Override
public int hashCode() {
int result = suit.hashCode();
result = 31 * result + rank.hashCode();
return result;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Card{");
sb.append("suit=").append(suit);
sb.append(", rank=").append(rank);
sb.append('}');
return sb.toString();
}
}
class Deck {
private List<Card> deck;
private Random random;
public Deck() {
this.init();
this.random = new Random();
}
public Deck(long seed) {
this.init();
this.random = new Random(seed);
}
private void init() {
this.deck = new ArrayList<Card>();
for (SUIT suit: SUIT.values()) {
for (RANK rank: RANK.values()) {
this.deck.add(new Card(suit, rank));
}
}
}
public Card deal() { return this.deal(true); }
public Card deal(boolean removeCard) {
int value = this.random.nextInt(this.deck.size());
return removeCard ? this.deck.remove(value) : this.deck.get(value);
}
}
包装卡;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
/**
*练习07_29
*@作者迈克尔
*@linkhttps://stackoverflow.com/questions/31639964/pick-four-cards-and-compute-their-sum-java
*@自2015年7月26日下午1:42
*/
公开课练习07_29{
公共静态最终int NUM_卡=4;
公共静态void main(字符串[]args){
甲板=新甲板();
List hand=new ArrayList();
智力得分=0;
对于(int i=0;i
导入java.util.*;
公开课练习07_29{
公共静态void main(字符串[]args){
//初始化一切
int[]甲板=新int[52];
字符串[]套装={“黑桃”、“红桃”、“钻石”、“梅花”};
字符串[]排名={“王牌”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”、“10”、“杰克”、“女王”、“国王”};
List pickedCards=new ArrayList();
//初始化卡
对于(int i=0;ipackage cards;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* Exercise07_29
* @author Michael
* @link https://stackoverflow.com/questions/31639964/pick-four-cards-and-compute-their-sum-java
* @since 7/26/2015 1:42 PM
*/
public class Exercise07_29 {
public static final int NUM_CARDS = 4;
public static void main(String[] args) {
Deck deck = new Deck();
List<Card> hand = new ArrayList<>();
int score = 0;
for (int i = 0; i < NUM_CARDS; ++i) {
Card card = deck.deal();
hand.add(card);
score += card.getRank().getValue();
}
System.out.println(hand);
System.out.println(score);
}
}
enum SUIT {
CLUB, DIAMOND, HEART, SPADE;
}
enum RANK {
ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);
private final int value;
RANK(int value) { this.value = value; }
public int getValue() {
return value;
}
}
class Card implements Comparable<Card> {
private final SUIT suit;
private final RANK rank;
public Card(SUIT suit, RANK rank) {
if (suit == null) throw new IllegalArgumentException("suit cannot be null");
if (rank == null) throw new IllegalArgumentException("rank cannot be null");
this.suit = suit;
this.rank = rank;
}
public SUIT getSuit() {
return suit;
}
public RANK getRank() {
return rank;
}
@Override
public int compareTo(Card other) {
if (this.getRank().equals(other.getRank())) {
return this.getSuit().compareTo(other.getSuit());
} else {
return this.getRank().getValue() - other.getRank().getValue();
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Card card = (Card) o;
return suit == card.suit && rank == card.rank;
}
@Override
public int hashCode() {
int result = suit.hashCode();
result = 31 * result + rank.hashCode();
return result;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Card{");
sb.append("suit=").append(suit);
sb.append(", rank=").append(rank);
sb.append('}');
return sb.toString();
}
}
class Deck {
private List<Card> deck;
private Random random;
public Deck() {
this.init();
this.random = new Random();
}
public Deck(long seed) {
this.init();
this.random = new Random(seed);
}
private void init() {
this.deck = new ArrayList<Card>();
for (SUIT suit: SUIT.values()) {
for (RANK rank: RANK.values()) {
this.deck.add(new Card(suit, rank));
}
}
}
public Card deal() { return this.deal(true); }
public Card deal(boolean removeCard) {
int value = this.random.nextInt(this.deck.size());
return removeCard ? this.deck.remove(value) : this.deck.get(value);
}
}
import java.util.*;
public class Exercise07_29 {
public static void main(String[] args){
//initialize everything
int[] deck = new int[52];
String[] suits = {"Spades", "Hearts", "Diamonds", "Clubs"};
String[] ranks = {"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
List<String> pickedCards = new ArrayList<String>();
//initialize the cards
for(int i = 0; i< deck.length; i ++)
deck[i] = i;
//shuffle the cards
for(int i = 0; i < deck.length; i++){
//generate an index randomly
int index = (int)(Math.random() * deck.length);
int temp = deck[i];
deck[i] = deck[index];
deck[index] = temp;
}
//display the four cards
for(int i = 0; i < 4; i++){
String suit = suits[deck[i] / 13];
String rank = ranks[deck[i] % 13];
System.out.println(rank + " of " + suit);
pickedCards.add(rank);
}
//initialize Ace Jack Queen King
int Ace, Jack, Queen, King;
//Assign a point vale to each
int[] points = {Ace = 1, Jack = 11, Queen = 12, King = 13};
//add the cards together and show output
int sum = 0;
int jack = 11;
int queen = 12;
int king = 13;
int ace = 1;
Iterator<String> iterator = pickedCards.iterator();
while(iterator.hasNext()) {
String rank = iterator.next();
System.out.println(rank);
if(rank.equalsIgnoreCase("Jack")){
sum = sum+jack;
}
else if(rank.equalsIgnoreCase("Queen")){
sum = sum+queen;
}
else if(rank.equalsIgnoreCase("King")){
sum = sum+king;
}
else if(rank.equalsIgnoreCase("Ace")){
sum = sum+ace;
}
else {
sum = sum+Integer.parseInt(rank);
}
}
System.out.println("Sum of picked cards is : "+sum);
}