Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 建筑纸牌游戏需要相同的纸牌来匹配价值_Java_Arrays - Fatal编程技术网

Java 建筑纸牌游戏需要相同的纸牌来匹配价值

Java 建筑纸牌游戏需要相同的纸牌来匹配价值,java,arrays,Java,Arrays,我正在为一项任务制作一个战争卡游戏,我已经制作了这个游戏,它正在运行,但是我遇到了一个问题,比如卡10黑桃杰克价值较低,比如卡23红桃杰克。寻找关于能够比较这些卡片的最佳方法的建议,看看它们是否相等 以下是我目前掌握的代码: import java.util.Random; import java.util.Scanner; public class WarGame { public static void main(String a[]) { DeckOfCards

我正在为一项任务制作一个战争卡游戏,我已经制作了这个游戏,它正在运行,但是我遇到了一个问题,比如卡10黑桃杰克价值较低,比如卡23红桃杰克。寻找关于能够比较这些卡片的最佳方法的建议,看看它们是否相等

以下是我目前掌握的代码:

import java.util.Random;
import java.util.Scanner;

public class WarGame {

    public static void main(String a[]) {
        DeckOfCards d = new DeckOfCards();
        int input = 0; 
        int computer = 0;
        int you = 0;
        Scanner sc = new Scanner(System.in);
        System.out.print("\n1.To play\n2.Exit\nEnter the choice:");
        input = sc.nextInt();
        while (input != 2) {
            if (input == 1) {
                System.out.print("\n\nEnter the value of the card:");
                int card = sc.nextInt();
                if (card >= 0 && card <= 51) {
                    int systemCard = d.computerTurn(card);
                    System.out.println("Your card - " + d.inputCard(card));
                    System.out.println("Computer card - " + d.inputCard(systemCard));
                    if(systemCard > card)
                        ++computer;
                    else
                        ++you;
                    System.out.println("The winner is " + (systemCard > card ? "Computer" : "You"));
                } else {
                    System.out.println("Invalid card");
                }
            }
            else {
            System.out.println("That is an invalid selection please choose 1 or 2.");
            }
            System.out.print("\n1.To play\n2.Exit\nEnter the choice:");
            input = sc.nextInt();
        }
        System.out.println("Total Wins by Computer: "+ computer);
        System.out.println("Total Wins by You: "+ you);
        if (computer > you)
            System.out.println("Computer is the champion");
        else if (computer == you)
            System.out.println("Its a Tie");
        else
            System.out.println("You are the champion");
    }
    }

    class DeckOfCards {
        String suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
        Random ran = new Random();
        int systemWin = 0;
        int playerWin = 0;
        String inputCard(int card) {
            int suit = card / 13; //assigning suit to your card
            int rank = card % 13; //Assigning rank to your card
            String out = "";
            switch (rank) { //Setting up face cards for the cases
            case 0:
                out = "Ace of " + suits[suit];
                break;
            case 10:
                out = "Jack of " + suits[suit];
                break;
            case 11:
                out = "Queen of " + suits[suit];
                break;
            case 12:
                out = "King of " + suits[suit];
                break;
            default:
                out = rank + 1 + " of " + suits[suit]; //Adding one to remainder so it will go from 2-10 instead of 1-9
                break;
            }
            return out;
        }

        int computerTurn(int playerRank) { //Keeping track of the wins for computer and player
            int systemRank = this.ran.nextInt(51);
            if (systemRank > playerRank)
                systemWin++;
            else
                playerWin++;
            return systemRank;
        }
    }
import java.util.Random;
导入java.util.Scanner;
公营作战游戏{
公共静态void main(字符串a[]{
DeckOfCards d=新的DeckOfCards();
int输入=0;
int计算机=0;
int=0;
扫描仪sc=新的扫描仪(System.in);
System.out.print(“\n1.播放\n2.退出\n输入选项:”);
输入=sc.nextInt();
while(输入!=2){
如果(输入=1){
System.out.print(“\n\n输入卡的值:”);
int card=sc.nextInt();
如果(卡>=0&卡)
++计算机;
其他的
++你;
System.out.println(“获胜者是”+(systemCard>card?“计算机”:“你”);
}否则{
System.out.println(“无效卡”);
}
}
否则{
System.out.println(“这是一个无效的选择,请选择1或2”);
}
System.out.print(“\n1.播放\n2.退出\n输入选项:”);
输入=sc.nextInt();
}
System.out.println(“通过计算机获得的总胜利:“+计算机”);
System.out.println(“你赢得的总胜利:+You”);
如果(计算机>你)
System.out.println(“计算机是冠军”);
else if(计算机==您)
System.out.println(“这是一个平局”);
其他的
System.out.println(“你是冠军”);
}
}
甲板类{
弦乐套装[]={“黑桃”、“红桃”、“钻石”、“梅花”};
Random ran=新的Random();
int systemWin=0;
int playerWin=0;
字符串输入卡(int卡){
int suit=card/13;//为您的卡分配suit
int rank=card%13;//为您的卡分配等级
串出“”;
切换(等级){//为案例设置人脸卡
案例0:
out=“Ace of”+套装[套装];
打破
案例10:
out=“Jack of”+套装[套装];
打破
案例11:
out=“女王”+套装[套装];
打破
案例12:
out=“国王”+套装[套装];
打破
违约:
out=rank+1+“of”+suites[suite];//在余数中添加一,使其从2-10变为1-9
打破
}
返回;
}
int computerTurn(int playterrak){//为计算机和玩家记录胜利
int systemRank=this.ran.nextInt(51);
如果(systemRank>playerRank)
systemWin++;
其他的
playerWin++;
返回系统等级;
}
}

我认为您是在将索引与卡片组进行比较,而不是将卡片值本身进行比较。如果我理解的话,您希望将d.inputCard(卡)与d.inputCard(系统卡)进行比较,而不是将卡与系统卡进行比较。但当然,这是一个字符串。很难遵循代码:-)。

您有计算卡片等级的代码(我在
DeckOfCards
类中看到),也许您应该比较这些,而不是整个卡片“编号”…对不起,我是编程新手是的,我想比较牌的等级,而不是在阵列中的位置,所以心之王等于黑桃之王等等。我试图编辑您建议的代码,但由于某种原因,它给了我一个错误。