Java老虎机代码拆分两类遇问题:Reel类spin方法调用异常求助
Java老虎机代码类拆分问题及修复方案
我完成了一个简易老虎机的Java代码,但对类的使用有疑惑。计划将随机生成符号的逻辑放到名为Reel的类的spin()方法中,但拆分后调用该方法时出现异常。以下是原代码及拆分后的代码,求指导正确的拆分方式。
原代码
import java.util.Scanner; import java.util.Random; import java.util.Arrays; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); final int numReels = 3; //Declare how many reels spin on machine Random random = new Random(); // Create Random int[ ] symbols = new int[numReels]; // Create an array of three random numbers String[ ] logo = new String[numReels]; // Second Array to assign numbers to symbols final String[] allSymbols = {"Cherry", "7", "Grape", "Orange", "Lemon", "Spade", "Club", "Diamond", "Heart", "Ace"}; // A final list of what symbols there are for (int i = 0; i < symbols.length; ++i) { // Loop through the integer list to obtain random numbers symbols[ i ] = random.nextInt(10); //Random numbers from 0 - 9 //System.out.println(symbols[i]); Test to make sure list is looping properly } for (int i = 0; i < symbols.length; ++i) { // Assign second array to the correct symbol according to number logo[i] = allSymbols[symbols[i]]; } //Test each element in list to make sure obtaining correct values //System.out.println("symbol #: " + symbols[0] + " logo: " + logo[0]); //System.out.println("symbol #: " + symbols[1] + " logo: " + logo[1]); //System.out.println("symbol #: " + symbols[2] + " logo: " + logo[2]); // Simplify each element by assigning to single letter variables int a = symbols[0]; int b = symbols[1]; int c = symbols[2]; String answer = "Y"; // Variable to be used in while loop (continue playing or not) int userBet; // Variable for amount user bets int doub; //Variable for reward when user matches two symbols int trip; //Variable for reward when user matches three symbols int jack; //Variable for reward when user matches three jackpot symbols int amountWon = 0; System.out.println("Welcome to the Simple Slot Machine! Enter a betting amount to spin!!"); // Print intro to user do { //do-while loop for when answer variable is Y or y userBet = scnr.nextInt(); // Take input for betting amount if (userBet < 10) { // Bet must be higher than $10 System.out.println("Bet must be higher then $10"); } if (a != b && a != c && b != c) { // When no symbols match the user loses amount that was bet System.out.println("You lost your $" + userBet); amountWon -= userBet; } else if (a == 1 && b == 1 && c == 1) { // When all jackpot symbols match user wins Jackpot and adds to winnings jack = userBet * 100; System.out.println("Congratulations! You have won with a " + logo[0] + ", " + logo[1] + ", " + logo[2] + " the jackpot of $" + jack); amountWon += jack; } else if (a == b && b == c) { // When three symbols match user wins triple award and adds to winnings trip = userBet * 70; System.out.println("Congratulations, you have won with a Triple!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + trip); amountWon += trip; } else if (a == b || a == c || b == c) { // When two symbols match user wins award and add to winnings doub = userBet * 30; System.out.println("Congratulations, you have won with a Double!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + doub); amountWon += doub; } System.out.println("Continue? Y/N "); // Once awards accounted for ask if player continues or not answer = scnr.next(); } while (answer.equals("y") || answer.equals("Y")); if (!answer.equals("Y")) { if (amountWon < 0) { //Print statement for when player is in the hold System.out.println("You ended losing " + amountWon +" dollars" + ", but people always stop before they hit it big!"); } else { // Print statement for when player is in positive System.out.println("You ended with $" + amountWon + " come back again!"); } } } }
拆分后代码
import java.util.Random; import java.util.Arrays; public class Reel { public void spin() { final int numReels = 3; //Declare how many reels spin on machine Random random = new Random(); // Create Random int[ ] symbols = new int[numReels]; // Create an array of three random numbers String[ ] logo = new String[numReels]; // Second Array to assign numbers to symbols final String[] allSymbols = {"Cherry", "7", "Grape", "Orange", "Lemon", "Spade", "Club", "Diamond", "Heart", "Ace"}; // A final list of what symbols there are for (int i = 0; i < symbols.length; ++i) { // Loop through the integer list to obtain random numbers symbols[ i ] = random.nextInt(10); //Random numbers from 0 - 9 //System.out.println(symbols[i]); Test to make sure list is looping properly } for (int i = 0; i < symbols.length; ++i) { // Assign second array to the correct symbol according to number logo[i] = allSymbols[symbols[i]]; } } } import java.util.Scanner; import java.util.Random; import java.util.Arrays; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Reel action = new Reel(); action.spin(); //Test each element in list to make sure obtaining correct values //System.out.println("symbol #: " + symbols[0] + " logo: " + logo[0]); //System.out.println("symbol #: " + symbols[1] + " logo: " + logo[1]); //System.out.println("symbol #: " + symbols[2] + " logo: " + logo[2]); // Simplify each element by assigning to single letter variables int a = symbols[0]; int b = symbols[1]; int c = symbols[2]; String answer = "Y"; // Variable to be used in while loop (continue playing or not) int userBet; // Variable for amount user bets int doub; //Variable for reward when user matches two symbols int trip; //Variable for reward when user matches three symbols int jack; //Variable for reward when user matches three jackpot symbols int amountWon = 0; System.out.println("Welcome to the Simple Slot Machine! Enter a betting amount to spin!!"); // Print intro to user do { //do-while loop for when answer variable is Y or y userBet = scnr.nextInt(); // Take input for betting amount if (userBet < 10) { // Bet must be higher than $10 System.out.println("Bet must be higher then $10"); } if (a != b && a != c && b != c) { // When no symbols match the user loses amount that was bet System.out.println("You lost your $" + userBet); amountWon -= userBet; } else if (a == 1 && b == 1 && c == 1) { // When all jackpot symbols match user wins Jackpot and adds to winnings jack = userBet * 100; System.out.println("Congratulations! You have won with a " + logo[0] + ", " + logo[1] + ", " + logo[2] + " the jackpot of $" + jack); amountWon += jack; } else if (a == b && b == c) { // When three symbols match user wins triple award and adds to winnings trip = userBet * 70; System.out.println("Congratulations, you have won with a Triple!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + trip); amountWon += trip; } else if (a == b || a == c || b == c) { // When two symbols match user wins award and add to winnings doub = userBet * 30; System.out.println("Congratulations, you have won with a Double!" + logo[0] + ", " + logo[1] + ", " + logo[2] + " $" + doub); amountWon += doub; } System.out.println("Continue? Y/N "); // Once awards accounted for ask if player continues or not answer = scnr.next(); } while (answer.equals("y") || answer.equals("Y")); if (!answer.equals("Y")) { if (amountWon < 0) { //Print statement for when player is in the hold System.out.println("You ended losing " + amountWon +" dollars" + ", but people always stop before they hit it big!"); } else { // Print statement for when player is in positive System.out.println("You ended with $" + amountWon + " come back again!"); } } } }
问题分析
拆分后的代码存在两个核心问题:
- 变量作用域问题:Reel类的spin()方法中,
symbols和logo是局部变量,LabProgram类无法直接访问这些变量,这会导致编译错误。 - 逻辑错误:原代码中只在程序启动时调用了一次
spin(),后续循环中没有重新生成新的符号,导致每次游戏都是同一组结果,不符合老虎机的逻辑。
正确的拆分方案
修改Reel类,将symbols和logo设为实例变量,并提供getter方法供外部访问;同时在LabProgram的循环内每次用户下注后重新调用spin()生成新结果。
修改后的Reel类
import java.util.Random; public class Reel { private final int numReels = 3; private final String[] allSymbols = {"Cherry", "7", "Grape", "Orange", "Lemon", "Spade", "Club", "Diamond", "Heart", "Ace"}; private int[] symbols; private String[] logo; private Random random; public Reel() { random = new Random(); symbols = new int[numReels]; logo = new String[numReels]; } public void spin() { // 生成随机符号编号 for (int i = 0; i < symbols.length; ++i) { symbols[i] = random.nextInt(10); } // 映射为符号名称 for (int i = 0; i < logo.length; ++i) { logo[i] = allSymbols[symbols[i]]; } } // Getter方法供外部获取结果 public int[] getSymbols() { return symbols; } public String[] getLogo() { return logo; } }
修改后的LabProgram类
import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Reel action = new Reel(); String answer = "Y"; int userBet; int amountWon = 0; System.out.println("Welcome to the Simple Slot Machine! Enter a betting amount to spin!!"); do { userBet = scnr.nextInt(); if (userBet < 10) { System.out.println("Bet must be higher than $10"); continue; // 跳过本次循环,重新要求下注 } // 每次下注后重新转动老虎机 action.spin(); int[] symbols = action.getSymbols(); String[] logo = action.getLogo(); int a = symbols[0]; int b = symbols[1]; int c = symbols[2]; // 判断中奖结果 if (a != b && a != c && b != c) { System.out.println("You lost your $" + userBet); amountWon -= userBet; } else if (a == 1 && b == 1 && c == 1) { int jack = userBet * 100; System.out.printf("Congratulations! You have won with a %s, %s, %s the jackpot of $%d%n", logo[0], logo[1], logo[2], jack); amountWon += jack; } else if (a == b && b == c) { int trip = userBet * 70; System.out.printf("Congratulations, you have won with a Triple! %s, %s, %s $%d%n", logo[0], logo[1], logo[2], trip); amountWon += trip; } else if (a == b || a == c || b == c) { int doub = userBet * 30; System.out.printf("Congratulations, you have won with a Double! %s, %s, %s $%d%n", logo[0], logo[1], logo[2], doub); amountWon += doub; } System.out.println("Continue? Y/N "); answer = scnr.next(); } while (answer.equalsIgnoreCase("Y")); if (amountWon < 0) { System.out.printf("You ended up losing %d dollars, but people always stop before they hit it big!%n", -amountWon); } else { System.out.printf("You ended with $%d come back again!%n", amountWon); } scnr.close(); } }
关键改进点
- 将Reel类的核心数据(symbols、logo)设为实例变量,通过getter方法暴露,解决作用域问题。
- 在每次用户下注后调用spin(),确保每次游戏结果都是随机的。
- 修复了原代码中下注金额检查后未跳过后续逻辑的问题,避免无效下注仍执行中奖判断。
- 使用
equalsIgnoreCase()简化继续游戏的判断逻辑,同时关闭Scanner资源,规范代码。
内容的提问来源于stack exchange,提问作者EliKhan
相关产品推荐
相关产品推荐

