我终于完成了我简单的老虎机代码,但是我很难理解类。我想把它分成两个类,不知道如何去做。我的想法是把前半部分做随机生成。创建一个名为Reels的类,并把代码放在public void spin的方法中().虽然,当我把它拆成另一个类并试图调用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.Scanner;
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!");
}
}
}
}
1条答案
按热度按时间fv2wmkja1#
您可以将您的问题视为与SlotMachine交互的用户,并且可以有一个类负责用户操作,而其他类负责SlotMachine功能和操作。用户将使用SlotMachine触发Slot。SlotMachine将执行卷轴数据填充。SlotMachine还将根据用户触发的金额来评估用户是否赢或输。
此外,我观察到有一个错误在你的方法,你填充插槽数据一次,但同时要求用户更多的赌注,你没有修改数据.请检查.