【问题标题】:Craps Project - Debugs for non apparent syntax errorsCraps Project - 调试不明显的语法错误
【发布时间】:2019-09-27 23:01:03
【问题描述】:

我在 Eclipse IDE 上使用 Java 制作了一个简单的 Craps 模拟器,我很难调试那些仅在编译过程中似乎没有表明语法错误的东西。

我在尝试为“桌子”上的第一个赌注分配值时遇到了这个问题。

Pass() 方法没有像我预期的那样运行,我没有任何立即的错误消息,但是它不想编译超过这一点,这只是 main 方法的第 33 行.

char answer = keyboard.nextLine().toUpperCase().charAt(0);这条线似乎是让我的所有方法在整个过程中顺利运行的最大障碍……据我所知。

我确实有一个 die 课程,但事实证明这不是问题,所以我没有将它包含在这篇文章中。

编辑:这是程序的全部内容。我一直在左右调整两端以找到合适的方法来编译这个绝对的混乱。

非常感谢任何输入。提前谢谢你。

import java.util.Scanner;


public class Craps {
    //Declaring start value of balances
    static double totalMoney = 0.00;
    static double rollMoney = 0.00;
    public static void main(String[] args) {
    //Creating Die Objects
        Die die1 = new Die();
        Die die2 = new Die();
    //Beginning of loop
    do {
    //Displaying the rules for the game
        displayMenu();
    //Array to hold both Dice and create total for the first roll
    //as well as every roll there after
    //Initialized 'purses' for the first bets 
        int dice[] = new int[2];
        int intTotal = 0;   
        int total = 0;
        int passBet = 2;
        double pass = 0.00;
        double dontPass = 0.00;
    //Rolling Dice for the first time in the Round
        die1.Roll();
        dice[0] = die1.getValue();
        die2.Roll();
        dice[1] = die2.getValue();
        intTotal = (dice[0] + dice[1]);
//Assigning Value to Pass/Don't Pass bets
    do {
        while(Pass() == true) {
            try{
                pass = getBet();
            } catch(NumberFormatException nfe) {
                System.out.println("Please answer in this numerical format (0.00)");
            }
        }
        while(dontPass() == true) {
            try{
                dontPass = getBet();
            } catch(NumberFormatException nfe) {
                System.out.println("Please answer in this numerical format (0.00)");
            }
        }
        //Display of each individual rolls
        //Display of total
        //Win-Loss-Continue Play check
        switch(intTotal) {
        case 7:
        case 11:
            rollMoney = (pass*passBet);
            dontPass = 0.00;
            System.out.println(dice[0] + " - " + dice[1]);
            System.out.println("Very Nice! Pay the Man!");
            System.out.println("Sorry Don't Pass line, better luck next time.");
            System.out.println("You have made: " + pass);
            System.out.println("Total Earnings: " + totalMoney);
        case 2:
        case 3:
        case 12:
            rollMoney = (dontPass*passBet);
            pass = 0.00;
            System.out.println(dice[0] + " - " + dice[1]);
            System.out.println("Very Nice! Pay the Man!");
            System.out.println("Sorry Pass line, better luck next time.");
            System.out.println("You have made: " + dontPass);
            System.out.println("Total Earnings: " + totalMoney);
        case 4:
        case 5:
        case 6:
        case 8:
        case 9:
        case 10:
            //"Actual" Game of Craps
        do {
        double placeBet[] = {1.2, 1.5, 2.0};
        int hardBet[] = {7, 9};
        int hornBet[] = {15, 30}; 

        //List<Double> place = new ArrayList<Double>();
        //List<Double> hard = new ArrayList<Double>();
        //List<Double> horn = new ArrayList<Double>();

        double place[] = new double[6];
        double hard[] = new double[4];
        double horn[] = new double[4];

        int pBetMax = 6;
        int haBetMax = 4;
        int hoBetMax = 4;

        int pBets[] = new int[pBetMax];
        int haBets[] = new int[haBetMax];
        int hoBets[] = new int[hoBetMax];

            //Gathering Place Bets w/ Location and Value
        if(Place() == true) {
        pBets = getPlace();
        for(int pBetOffset = 1; pBetOffset <= pBetMax; pBetOffset++) {
        switch(pBets[pBetOffset]) {
        case 4:
            place[0] += getBet();
        case 5:
            place[1] += getBet();
        case 6:
            place[2] += getBet();
        case 8:
            place[3] += getBet();
        case 9:
            place[4] += getBet();
        case 10:
            place[5] += getBet();
        case 0:
            break;
        }
    }
}
            //Gathering Hardway Bets w/ Location and Value
        if(Hard() == true) {
        haBets = getHard();
        for(int haBetOffset = 1; haBetOffset <= haBetMax; haBetOffset++) {
        switch(haBets[haBetOffset]) {
        case 4:
            hard[0] += getBet();
        case 6:
            hard[1] += getBet();
        case 8:
            hard[2] += getBet();
        case 10:
            hard[3] += getBet();
        case 0:
            break;
        }
    }
}               
            //Gathering Horn Bets w/ Location and Value
        if(Horn() == true) {
        hoBets = getHorn();
        for(int hoBetOffset = 1; hoBetOffset <= hoBetMax; hoBetOffset++) {
        switch(hoBets[hoBetOffset]) {
        case 2:
            horn[0] += getBet();
        case 3:
            horn[1] += getBet();
        case 11:
            horn[2] += getBet();
        case 12:
            horn[3] += getBet();
        case 0:
            break;
        }
    }
}           
        //Redefining the roll separate from the intRoll
        dice = new int[2];
        die1.Roll();
        dice[0] = die1.getValue();
        die2.Roll();
        dice[1]  = die2.getValue();
        total = (dice[0] + dice[1]);
        if(intTotal != total) {
        switch(total) {
            case 11:
                rollMoney += (horn[2] * hornBet[1]);
            case 2:
                rollMoney += (horn[0] * hornBet[2]);
            case 3:
                rollMoney += (horn[1] * hornBet[1]);
            case 12:
                rollMoney += (horn[3] * hornBet[2]);
            case 4:
                if(dice[0]== dice[1]) {rollMoney += (hard[0] * hardBet[1]);}
                rollMoney += (place[0] * placeBet[3]);
            case 5:
                rollMoney += (place[1] * placeBet[2]);
            case 6:
                if(dice[0]== dice[1]) {rollMoney += (hard[1] * hardBet[2]);}
                rollMoney += (place[2] * placeBet[1]);
            case 8:
                if(dice[0]== dice[1]) {rollMoney += (haBets[2] * hardBet[2]); }
                rollMoney += (place[3] * placeBet[1]);
            case 9:
                rollMoney += (place[4] * placeBet[2]);
            case 10:
                if(dice[0]== dice[1]) {rollMoney += (haBets[3] * hardBet[1]);}
                rollMoney += (place[5] * placeBet[3]);
            case 7:
                //Wiping the bets clean off the board
                pass = 0.00;
                place = new double[10];
                hard = new double[10];
                horn = new double[10];
                //Redefining the initial roll for the game
                intTotal = 0;
                total = 0;
                totalMoney += (dontPass * 2);
                crapOut();
            }
        }   
        }while(crapOut() == true);
        }
    }while(keepPlaying() == true);
    }while(displayMenu() == true);
}
    /*
     * This method determines if the player will continue after the round is over
     */
    public static boolean keepPlaying() {
        Scanner keyboard = new Scanner(System.in);
        boolean playAgain = false;
        System.out.println("");
        System.out.println("Do you want to roll some more? (Y/N)");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        playAgain = answer == 'Y';
        keyboard.close();
        return playAgain;
    }

    public static boolean crapOut() {
        Scanner keyboard = new Scanner(System.in);
        boolean playAgain = false;
        System.out.println("Crap Out! All Bets are OFF!");
        System.out.println("After this roll you now have: $" + rollMoney + " on the board!");
        System.out.println("Which bring's your grand total to.. $" + (totalMoney + rollMoney));
        System.out.println("Care to make another wager?");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        playAgain = answer == 'Y';
        keyboard.close();
        return playAgain;
    }
    /*
     * This method will assign values to all the bets available to the player
     */
    public static double getBet() {
        System.out.println("How much would you like to bet?");
        Scanner keyboard = new Scanner(System.in);
        String bet = keyboard.nextLine();
        float num;
        num = Float.parseFloat(bet);
        keyboard.close();
        return num;
    }
    /*
     * This method will ask if the player would like to Accept or Press their bet
     */
    public static boolean pressBet(int bet) {
        Scanner keyboard = new Scanner(System.in);
        boolean press = false;
        System.out.println("Press your bet? (Double your initial wager)");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        press = answer == 'Y';
        bet = bet * 2;
        keyboard.close();
        return press;
    }
    /*
     * Methods to check if play would like to place wager
     * Methods to assign those bets to appropriate places on the table
     */
    public static boolean Pass() {
        Scanner keyboard = new Scanner(System.in);
        boolean pass = false;
        System.out.println("Would you like to make a Pass Line Bet?");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        pass = answer == 'y';
        keyboard.close();
        return pass;
    }

    public static boolean dontPass() {
        Scanner keyboard = new Scanner(System.in);
        boolean dontPass = false;
        System.out.println("Would you like to make a Don't Pass Line Bet?");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        dontPass = answer == 'Y';
        keyboard.close();
        return dontPass;
    }

    public static boolean Place() {
        Scanner keyboard = new Scanner(System.in);
        boolean place = false;
        System.out.println("Would you like to make a Place Bet?");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        place = answer == 'Y';
        keyboard.close();
        return place;
    }

    public static int[] getPlace() {
        int counter = 1;
        int max = 6;
        Scanner keyboard = new Scanner(System.in);
        int[] select = new int[6];

        do {
        System.out.println("Make selections from the numbers (4, 5, 6, 8, 9, 10), Enter 0 to exit.");
        System.out.println("Enter bet and hit return for each selection."); 

        select[counter] = keyboard.nextInt();
        counter++;  
        } while(counter <= max || select[counter] != 0);

        keyboard.close();
        return select;
    }

    public static boolean Horn() {
        Scanner keyboard = new Scanner(System.in);
        boolean horn = false;
        System.out.println("Would you like to make a Pass Line Bet?");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        horn = answer == 'Y';
        keyboard.close();
        return horn;
    }

    public static int[] getHorn() {
        int counter = 1;
        int max = 4;
        Scanner keyboard = new Scanner(System.in);
        int[] select = new int[4];

        do {
        System.out.println("Make selections from the numbers (2, 3, 11, 12), Enter 0 to exit.");
        System.out.println("Enter bet and hit return for each selection."); 

        select[counter] = keyboard.nextInt();
        counter++;  
        } while(counter <= max || select[counter] != 0);

        keyboard.close();
        return select;
    }

    public static boolean Hard() {
        Scanner keyboard = new Scanner(System.in);
        boolean hard = false;
        System.out.println("Would you like to make a Hardway Bet?");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        hard = answer == 'Y';
        keyboard.close();
        return hard;
    }

    public static int[] getHard() {
        int counter = 1;
        int max = 4;
        Scanner keyboard = new Scanner(System.in);
        int[] select = new int[4];

        do {
        System.out.println("Make a selection from the numbers (4 (2&2), 6 (3&3), 8(4&4), 10(5&5)");     
        System.out.println("Enter bet and hit return for each selection."); 

        select[counter] = keyboard.nextInt();
        counter++;  
        } while(counter <= max || select[counter] != 0);

        keyboard.close();
        return select;
    }
    /*
     * Method to display rules and parameters of game prior to playing
     */
    public static boolean displayMenu() {
        System.out.println("Welcome to the Crosby Casino!");
        System.out.println("The Game is Craps, Rules Are:");
        System.out.println("If a 7 or 11 is the first roll on the table,");
        System.out.println("Pass bets get paid on the first roll!");
        System.out.println("If a 2, 3, or 12 is the first roll on the table, ");
        System.out.println("Don't pass bets get paid on the first roll!");
        System.out.println("If any other value is rolled, the game begins..");
        System.out.println("Pass bets believe whatever other value was rolled will appear before the next 7 does.");
        System.out.println("Don't Pass bets believe that the 7 will appear before the initial roll's value is shown again.");
        System.out.println("During the duration of the roll you can make up to 3 separate side bets");
        System.out.println("'Place bets' - (2, 5, 6, 8, 9, 10) Which pay out every time these numbers appear following the initial roll");
        System.out.println("'Hard bets' -  (4, 6, 8, 10) Which has the most coverage of all potential outcomes");
        System.out.println(" & 'Horn bets' - (2, 3, 11, 12) The best payout odds on the table");
        System.out.println("The table minimum is $5.00 to play");
        System.out.println("All side bet minimums are $1.00 to play");
        Scanner keyboard = new Scanner(System.in);
        boolean menu = false;
        System.out.println("Would you like play?");
        char answer = keyboard.nextLine().toUpperCase().charAt(0);
        menu = answer == 'Y';
        keyboard.close();
        return menu;
    }

}

【问题讨论】:

  • 请发布您的主函数和“骰子”数组声明,并准确指定异常在哪一行触发
  • 请见上文^^
  • 你的主要函数在哪里声明类似int dice[] = new int[2]的东西?从您的帖子中,我猜您仍然收到 ArrayIndexOutOfBounds 异常......在哪一行,这里:dice[1] = die1.getValue(); ?
  • 用完整代码重新格式化帖子,因为它存在^^

标签: java arrays for-loop boolean nosuchelementexception


【解决方案1】:

Java 数组是0-origin。基本上,这意味着数组中的第一个元素的索引为0,而不是1,正如您的dice 数组所示。因此,您需要将dice[1]dice[2] 分别替换为dice[0]dice[1]

编辑基于来自 cmets 的新信息:

这听起来像是您之前描述的 AIOOBE 的新错误。您的代码的问题是您需要确保在调用nextLine() 方法之前使用ScannerhasNextLine() 方法。本质上,当没有“下一行”时,您正在调用nextLine(),从而导致NoSuchElementException

【讨论】:

  • 我实际上只是完全取消了dice[] 数组,但我仍然收到同样的错误。我将更换阵列,看看是否能解决问题。
  • 能否请您添加堆栈跟踪(以及必要时引发异常的相关代码行)?
  • Exception in thread "main" java.util.NoSuchElementException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at Craps.Pass(Craps.java:267) at Craps.main(Craps.java:33)
  • char answer = keyboard.next().toUpperCase().charAt(0); + char answer = keyboard.nextLine().toUpperCase().charAt(0); 都产生相同的异常
  • 根据此新信息修改答案。
猜你喜欢
  • 1970-01-01
  • 2012-07-15
  • 2016-04-01
  • 2023-03-18
  • 1970-01-01
  • 1970-01-01
  • 2014-08-05
  • 2013-08-23
相关资源
最近更新 更多