Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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 - Fatal编程技术网

需要帮助在java中正确打印值而不重复它们吗

需要帮助在java中正确打印值而不重复它们吗,java,Java,我有一个程序,用户在其中输入卡集的大小,从1到52,当调用printSet()时,需要打印x金额卡。之后,调用bubbleSort()对值进行排序,再次调用printSet()以打印带有西服的值,但Jack、Queen、King和Ace似乎被一个数字替换,有趣的是,并不是所有的值都被更改为一个数字,有时它们打印得很完美。当卡片组的大小增加到52时,值和套色会在打印时重复 下面是一个示例来演示这些问题: 杰克、皇后、国王和王牌问题: 输入(卡套大小):10 输出: (第一次调用printSet时)

我有一个程序,用户在其中输入卡集的大小,从1到52,当调用printSet()时,需要打印x金额卡。之后,调用bubbleSort()对值进行排序,再次调用printSet()以打印带有西服的值,但Jack、Queen、King和Ace似乎被一个数字替换,有趣的是,并不是所有的值都被更改为一个数字,有时它们打印得很完美。当卡片组的大小增加到52时,值和套色会在打印时重复

下面是一个示例来演示这些问题:

杰克、皇后、国王和王牌问题:

输入(卡套大小):10

输出:

(第一次调用printSet时):

(第二次调用printSet时):

这些值和值存在重复问题:

输入(卡套大小):52

因此,我面临的问题部分的主要代码如下:

    public CardSet(int howMany){
        PlayingCard = new PlayingCard[howMany];
        startCount = howMany;
        Random rand = new Random();
        int count;
        for(count = 0; count < PlayingCard.length; count++){
            int x = rand.nextInt(13) + 1;
            int y = rand.nextInt(4) + 0;
            PlayingCard[count] = new PlayingCard(y, x);
            PlayingCard pc = new PlayingCard();

        }
    }

    public void printSet(){
        System.out.println("\t-------------------------");
        for(int i = 0; i < PlayingCard.length; i++) {
            System.out.println(PlayingCard[i]);
        }
    }
AlmostGame.java

/* ---------------------------------------------
    Modify the PlayingCard class and write the 
    CardSet class that will allow the 
    AlmostGame demo class to deal a user-specified
    number of unduplicated cards
    
    Use the class diagram below (new method as marked):
    ----------------------------------------
        PlayingCard
    ----------------------------------------
    - suit : String
    - value : int   
    ----------------------------------------
    + PlayingCard()
    + PlayingCard(aSuit : String, aValue : int)
NEW + PlayingCard(aSuit : int, aValue : int)
    + getSuit() : String
    + getValue() : int
    + setSuit(aSuit:String) : void
    + setValue(aValue:int) : void
    + toString() : String
NEW + equals(aCard : PlayingCard) : boolean
    ----------------------------------------

    --------------------------
NEW     CardSet
    ---------------------------
    - set : PlayingCard[]
    - startCount : int
    - rand_ : Random  (static member, can't underline here)
    ---------------------------
    + CardSet(howMany : int)
    + printSet() : void
    + bubbleSort() : void
    ---------------------------
    
    You should not alter the AlmostGame class (this one)
    
------------------------------------------------ */

import java.util.*;
/**
 * A class that randomly creates a CardSetof unduplicated PlayingCards,
 * prints the set, sorts the set and prints it again
 * @author CSC251
 */
public class AlmostGame
{
    /**
     * This method runs the AlmostGame class
     * @param args unused
     */
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int size = 0;
        while(size >52 || size < 1)
        {
            System.out.print("\nEnter size of cardset (between 1 and 52): ");
            size = sc.nextInt();
            sc.nextLine();
        }
        CardSet hand1 = new CardSet(size);
        hand1.printSet();
        hand1.bubbleSort();
        hand1.printSet();
        System.out.println("\nThanks for playing...\n");
    }
}
/*---------------------------------------------
修改PlayingCard类并编写
CardSet类,该类将允许
AlmostGame演示类处理用户指定的
未复制的卡数
使用下面的类图(标记的新方法):
----------------------------------------
打牌
----------------------------------------
-西装:细绳
-值:int
----------------------------------------
+玩牌
+播放卡(aSuit:String,aValue:int)
新+游戏卡(aSuit:int,aValue:int)
+getSuit():字符串
+getValue():int
+setSuit(aSuit:String):无效
+设置值(aValue:int):无效
+toString():字符串
新+等于(aCard:PlayingCard):布尔值
----------------------------------------
--------------------------
新卡片组
---------------------------
-设置:播放卡片[]
-起始计数:int
-随机(静态成员,此处不能下划线)
---------------------------
+卡片组(数量:整数)
+printSet():void
+bubbleSort():void
---------------------------
您不应该更改AlmostGame类(此类)
------------------------------------------------ */
导入java.util.*;
/**
*一个类,它随机创建一组未复制的游戏卡,
*打印集合,对集合进行排序,然后再次打印
*@作者CSC251
*/
公共类游戏
{
/**
*此方法运行AlmostGame类
*@param args未使用
*/
公共静态void main(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
int size=0;
而(尺寸>52 | |尺寸<1)
{
系统输出打印(“\n输入卡组的大小(介于1和52之间):”;
尺寸=sc.nextInt();
sc.nextLine();
}
CardSet hand1=新的CardSet(尺寸);
hand1.printSet();
hand1.bubbleSort();
hand1.printSet();
System.out.println(“\n用于播放…\n的等级”);
}
}

请在此处发布您的代码,并提供有关问题的详细信息。例如,如果输入2,您应该获得多少张卡?请不要提供指向您的代码的链接,尤其是需要电子邮件地址和密码才能查看的代码。将问题的相关代码放在你的帖子中。我已经用code@DevilsHnd很抱歉,我共享了一个代码链接,以备需要,但我认为邀请链接不需要电子邮件和密码即可查看,我已经更新了链接并测试了它,可以说它不需要电子邮件和密码来查看它
You Picked: 10 of Hearts
You Picked: 10 of Hearts
You Picked: 3 of Spades
You Picked: 7 of Clubs
You Picked: 7 of Clubs
You Picked: Ace of Spades
You Picked: Ace of Clubs
You Picked: 0 of Clubs
You Picked: 6 of Spades
You Picked: 2 of Hearts
...
    public CardSet(int howMany){
        PlayingCard = new PlayingCard[howMany];
        startCount = howMany;
        Random rand = new Random();
        int count;
        for(count = 0; count < PlayingCard.length; count++){
            int x = rand.nextInt(13) + 1;
            int y = rand.nextInt(4) + 0;
            PlayingCard[count] = new PlayingCard(y, x);
            PlayingCard pc = new PlayingCard();

        }
    }

    public void printSet(){
        System.out.println("\t-------------------------");
        for(int i = 0; i < PlayingCard.length; i++) {
            System.out.println(PlayingCard[i]);
        }
    }
import java.util.*;

public class CardSet{
    private PlayingCard[] PlayingCard;
    int startCount;
    private Random rand;

    public CardSet(int howMany){
        PlayingCard = new PlayingCard[howMany];
        startCount = howMany;
        Random rand = new Random();
        int count;
        for(count = 0; count < PlayingCard.length; count++){
            int x = rand.nextInt(13) + 1;
            int y = rand.nextInt(4) + 0;
            PlayingCard[count] = new PlayingCard(y, x);
            PlayingCard pc = new PlayingCard();

        }
    }

    public void printSet(){
        System.out.println("\t-----");
        for(int i = 0; i < PlayingCard.length; i++) {
            System.out.println(PlayingCard[i]);
        }
    }

    public void bubbleSort(){
        PlayingCard _pc = new PlayingCard();
        /*
        for(int i = 1; i < PlayingCard.length; i++){
            for(int j = PlayingCard.length - 1; j >= i; j--){
                if(PlayingCard[j - 1].getValue() > (PlayingCard[j].getValue())){
                    _pc = PlayingCard[j - 1];
                    PlayingCard[j - 1] = PlayingCard[j];
                    PlayingCard[j] = _pc;
                }
            }
        }
        */
    }
}

public class PlayingCard {
    String suit;
    private int value;
    public static final String Suits[] = {"Hearts", "Clubs", "Diamonds", "Spades"};
    public static final String face_suits[] = {"Jack", "Queen", "King", "Ace"};
    public static final int values[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};

    PlayingCard(){
        suit = "";
        value = 0;
    }
    PlayingCard(String aSuit, int aValue){
            this.suit = aSuit;
            this.value = aValue;
    }

    PlayingCard(int aSuit, int aValue){
        suit = Suits[aSuit];
        value = aValue;
    }

    public void setValue(int aValue){
        value = aValue;
    }

    public void setSuit(String aSuit){
        suit = aSuit;
    }

    public int getValue(){
        return this.value;
    }

    public String getSuit(){
        return this.suit;
    }

    public String toString(){
        if(value == 1){
            String sValue = face_suits[3];
            return "\tYou Picked: " + sValue + " of " + suit;
        }
        if (value > 10){
            value = value - 11;
            String sValue = face_suits[value];
            return "\tYou Picked: " + sValue + " of " + suit;
        } else{
            return "\tYou Picked: " + value + " of " + suit;
        }
    }

    public boolean equals(PlayingCard aCard) {
        return (this.suit.equals(aCard.suit)
        && this.value == aCard.value)? true : false;
    }
}

/* ---------------------------------------------
    Modify the PlayingCard class and write the 
    CardSet class that will allow the 
    AlmostGame demo class to deal a user-specified
    number of unduplicated cards
    
    Use the class diagram below (new method as marked):
    ----------------------------------------
        PlayingCard
    ----------------------------------------
    - suit : String
    - value : int   
    ----------------------------------------
    + PlayingCard()
    + PlayingCard(aSuit : String, aValue : int)
NEW + PlayingCard(aSuit : int, aValue : int)
    + getSuit() : String
    + getValue() : int
    + setSuit(aSuit:String) : void
    + setValue(aValue:int) : void
    + toString() : String
NEW + equals(aCard : PlayingCard) : boolean
    ----------------------------------------

    --------------------------
NEW     CardSet
    ---------------------------
    - set : PlayingCard[]
    - startCount : int
    - rand_ : Random  (static member, can't underline here)
    ---------------------------
    + CardSet(howMany : int)
    + printSet() : void
    + bubbleSort() : void
    ---------------------------
    
    You should not alter the AlmostGame class (this one)
    
------------------------------------------------ */

import java.util.*;
/**
 * A class that randomly creates a CardSetof unduplicated PlayingCards,
 * prints the set, sorts the set and prints it again
 * @author CSC251
 */
public class AlmostGame
{
    /**
     * This method runs the AlmostGame class
     * @param args unused
     */
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int size = 0;
        while(size >52 || size < 1)
        {
            System.out.print("\nEnter size of cardset (between 1 and 52): ");
            size = sc.nextInt();
            sc.nextLine();
        }
        CardSet hand1 = new CardSet(size);
        hand1.printSet();
        hand1.bubbleSort();
        hand1.printSet();
        System.out.println("\nThanks for playing...\n");
    }
}