【问题标题】:Program executing and stops for no reason程序无故执行和停止
【发布时间】:2017-03-30 00:49:21
【问题描述】:

所以我为我的作业做了一个扑克游戏。
一切正常(至少我相信如此)。简单来说:玩家有 100 美元的余额,根据他的手牌,赢或输钱。
我做了一个条件,只要玩家的余额高于,游戏就会不断为玩家提供新手。
但是,在 7 或 8 次重新启动后,程序只是显示为白色,并且无论您输入什么,它都会一直运行...

这里是代码希望大家帮忙,另外,如果大部分代码是法语的,我很抱歉,我用英文注释了方法以帮助你理解:

package pkgtp2;

import java.util.*;

public class TP2 {

static boolean straight;
static boolean flush;
static int paires;
static int gains;
static int balance = 100;
static char[] cardsSymbols = new char[5];
static String[] cardsValues = new String[5];
static int hand[] = new int[5];
static boolean pack[] = new boolean[52];

public static void menu(int gains) {
    System.out.println("**************            " + " AUCUNE COMBINAISON = -10$");
    System.out.println("*Jeu de Poker*            " + " 1 PAIRE = 0$");
    System.out.println("**************            " + " 2 PAIRES = 20$");
    System.out.println("                          " + " BRELAN <3> = 35$");
    System.out.println("                          " + " SUITE = 50$");
    System.out.println("                          " + " FULL = 75$");
    System.out.println("                          " + " COULEUR = 100$");
    System.out.println("                          " + " CARRÉ = 150$");
    System.out.println("                          " + " STRAIGHT FLUSH = 500$");
} // The Game Menu showing every possible hand

public static void drawCards(int[] hand, boolean[] pack) {

    Random give = new Random();

    for (int i = 0; i < hand.length; i++) {
        do {
            hand[i] = give.nextInt(52);
        } while (pack[hand[i]]);
        pack[hand[i]] = true;
    }
} // Gives the user 5 unique random numbers

public static void cardSymbol(int[] cards, char[] cardssymbols) {
    char symboles[] = {'♥', '♦', '♣', '♠'};
    int numOfSymbol;
    for (int i = 0; i < cards.length; i++) {
        numOfSymbol = cards[i] / 13;
        cardsSymbols[i] = symboles[numOfSymbol];
    }

} // Converts the 5 numbers into values

public static void cardValue(int[] cards, String[] cardsvalues) {
    String valeurs[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    int numOfValues;
    for (int i = 0; i < cards.length; i++) {
        numOfValues = cards[i] % 13;
        cardsValues[i] = valeurs[numOfValues];
    }
} // Converts the 5 numbers into actual cards values

public static void printCards(char[] cardsymbols, String[] cardsvalues) {
    System.out.println("Voici vos cartes : \n");
    for (int i = 0; i < hand.length; i++) {
        System.out.print(cardsymbols[i]);
        System.out.print(cardsvalues[i] + "   ");
    }
} // Prints the Cards

public static void changeCards(int[] cards, boolean[] pack) {
    Scanner read = new Scanner(System.in);
    int cardchange = 0;
    Random give = new Random();
    System.out.println();

    for (int i = 0; i < 4; i++) {
        do {
            System.out.println(" Entrez des chiffres de 1 à 5 correspondant aux cartes que vous désirez changer (vous pouvez changer au plus 4 cartes)"
                    + " \n Si vous voulez conserver vos cartes, entrez 0 ");
            while (!read.hasNextInt()) {
                System.out.println("Veuillez entrer un chiffre ");
                read.next();
            }
            cardchange = read.nextInt();
        } while (cardchange < 0 || cardchange > 5);

        if (cardchange == 0) {
            i = 5;
        } else {
            System.out.print("\nLa carte  " + cardchange + " va être changée\n ");
            do {
                cards[cardchange - 1] = give.nextInt(52);
            } while (pack[cards[cardchange - 1]]);
            pack[cards[cardchange - 1]] = true;
        }

        cardSymbol(cards, cardsSymbols);
        cardValue(cards, cardsValues);
        printCards(cardsSymbols, cardsValues);

    }

} // Lets the user change up to 4 cards, press 0 to not change any card and skip

public static int checkPairs(char[] cardsSymbols, String[] cardsvalues) {
    int paires = 0;
    for (int i = 0; i < cardsvalues.length; i++) {
        for (int j = i + 1; j < cardsvalues.length; j++) {
            if (cardsvalues[i].equals(cardsvalues[j])) {
                paires++;
            }
        }
    }
    return paires;
} // Checks if the cards values match so the program can determine if the user has a pair, two pair, three of a kind, full house or quads

public static boolean checkFlush(char[] cardsSymbols) {
    boolean flush = false;
    if (cardsSymbols[0] == cardsSymbols[1] && cardsSymbols[1] == cardsSymbols[2] && cardsSymbols[2] == cardsSymbols[3] && cardsSymbols[3] == cardsSymbols[4]) {
        flush = true;
    }
    return flush;
} // checks for a flush

public static boolean checkStraight(String[] cardsvalues) {
    boolean straight = false;
    int numOfValues;
    int[] straightTab = new int[5];
    for (int i = 0; i < hand.length; i++) {
        numOfValues = hand[i] % 13;
        straightTab[i] = numOfValues;
    }
    Arrays.sort(straightTab);

    if (straightTab[0] + 1 == straightTab[1] && straightTab[1] + 1 == straightTab[2] && straightTab[2] + 1 == straightTab[3] && straightTab[3] + 1 == straightTab[4]) {
        straight = true;
    }
    return straight;
} // checks for a straight

public static int combinaisons(int paires, boolean straight, boolean flush) {
    int gains;
    if (straight && flush) {
        System.out.println("Vous avez une Quinte !");
        gains = 500;
    } else if (flush) {
        System.out.println("Vous avez une Couleur !");
        gains = 100;
    } else if (straight) {
        System.out.println("Vous avez une Suite !");
        gains = 50;
    } else if (paires == 6) {
        System.out.println("Vous avez un Carré !");
        gains = 150;
    } else if (paires == 4) {
        System.out.println("Vous avez un Full !");
        gains = 75;
    } else if (paires == 3) {
        System.out.println("Vous avez un Brelan !");
        gains = 35;
    } else if (paires == 2) {
        System.out.println("Vous avez Deux Paires !");
        gains = 20;
    } else if (paires == 1) {
        System.out.println("Vous avez une Paire !");
        gains = 0;
    } else {
        System.out.println("Vous n'avez Aucune Combinaison !");
        gains = -10;
    }
    return gains;
} // gives the user money based on his hand strength

public static int profit(int gains) {
    balance = balance + gains;
    System.out.println("Vos gains sont  de " + gains + " $");
    System.out.println("Vous avez donc " + balance + " $");
    return gains;
} // Gives the user his profit and his new balance

public static void main(String[] args) { 
    do {
        menu(gains);
        drawCards(hand, pack);
        cardSymbol(hand, cardsSymbols);
        cardValue(hand, cardsValues);
        printCards(cardsSymbols, cardsValues);
        changeCards(hand, pack);
        paires = checkPairs(cardsSymbols, cardsValues);
        flush = checkFlush(cardsSymbols);
        straight = checkStraight(cardsValues);
        gains = combinaisons(paires, straight, flush);
        profit(gains);
    }while (balance > 0);
    }// Here is my problem, the program just stops for a reason ...

}

【问题讨论】:

  • 我可以假设您没有任何控制台错误...?
  • 我不相信我有,您可以快速复制/粘贴代码并自己尝试一下
  • 对不起,伙计。我完全错过了这是 Java 不是 Javascript。我的错。
  • 最后是一个尾括号吗?
  • 它现在编译得很好。我刚刚尝试过,它在 9 次左右重新启动后停止,正如 OP 提到的那样。

标签: java loops conditional-statements


【解决方案1】:

drawCards 方法中,pack[hand[i] = true 最终(经过 10 手左右)将所有pack[hand[i] 设置为true,导致while (pack[hand[i]]); 进入无限循环。

pack[hand[i]] = true; 更改为pack[hand[i]] = false; 可防止无限循环并允许继续玩游戏。

正如 OP 在 cmets 中所建议的,一种方法是在每一轮之后重置“pack”数组,这可以通过以下方法完成:

public static void reset_pack(){
    for(int i=0; i<pack.length;i++){
        pack[i] = false;
    }
}

并在每一轮后调用此方法:

...
profit(gains);
reset_pack();

【讨论】:

  • 谢谢,这可行,但它会使我的代码出现错误,因为同一张卡可能会生成两次......关于如何在每一轮后重置“pack”数组的任何想法?
  • 哦,好点!感谢您指出这一点,因为我没有测试我的逻辑,并且确实重置“pack”数组将是一种更好的方法。也许重置“pack”数组的方法会起作用。我将编辑我的答案并添加代码来重置数组。
  • 没关系!我解决了问题谢谢你的帮助!
  • 太好了,很高兴听到你自己修好了 ;-) Santé!
【解决方案2】:

感谢 downshift 指出它会造成无限循环。
这是我需要添加的内容:

for (int i=0;i<hand.length;i++){
pack[hand[i]] = false;
}

在“changeCards”方法之后添加该条件可以让程序重置您在接下来的回合中不再需要的“pack”数组。这意味着程序可以为下一轮再次选择值,因此不会在 8 或 9 次尝试后停止!
谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-18
    • 2011-01-24
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多