如何在程序中为随机数分配一个字符串值

Ste*_*Eck 3 java random if-statement

正如标题所示,我正在做一个作为老虎机的家庭作业计划.我已经四处寻找,我很满意该程序对我来说足够正常.我有的问题是生成随机数,我应该为数字1-5分配值(樱桃,橘子,李子,铃,甜瓜,酒吧).然后我在程序运行时显示输出而不是数字.谁能让我指出如何做到这一点的正确方向?

import java.util.Random;
import java.util.Scanner;



public class SlotMachineClass {


public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int Coins = 1000;
    int Wager = 0;




    System.out.println("Steve's Slot Machine");
    System.out.println("You have " + Coins + " coins.");
    System.out.println("Enter your bet and press Enter to play");




    while (Coins > 0)
    {
    int first = new Random().nextInt(5)+1;
    int second = new Random().nextInt(5)+1;
    int third = new Random().nextInt(5)+1;

    Wager = input.nextInt();

    if(Wager > Coins)
             Wager = Coins;

    System.out.println(first + " " + second + " " + third);


    if(first == second && second == third)
    { Coins = Coins + (Wager * 3);
         System.out.println("You won " + (Wager * 3) + "!!!!" + " You now have " + Coins + " coins.");
         System.out.println("Enter another bet or close program to exit");}

    else if((first == second && first != third) || (first != second && first == third) || (first != second && second == third))
    { Coins = Coins + (Wager * 2);
         System.out.println("You won " + (Wager * 2) + "!!!" + " You now have " + Coins + " coins.");
         System.out.println("Enter another bet or close program to exit");}

    else {Coins = Coins - Wager;  
    System.out.println("You Lost!" + "\nPlay Again? if so Enter your bet.");}



    }

    while (Wager == 0)
    {
        System.out.println("You ran out of coins. Thanks for playing."); 
    }


}
Run Code Online (Sandbox Code Playgroud)

}

Tod*_*odd 5

如果您有int并希望String与之相关联,那么有几种方法可以做到这一点.

第一个是拥有一个字符串数组并查找它们.

public static String[] text = new String[] {"Cherry", "Bell", "Lemon", "Bar", "Seven"};
public String getNameForReel(int reelValue) {
    return text[reelValue];
}
// And to call it...
System.out.println(getNameForReel(first)); //etc...
Run Code Online (Sandbox Code Playgroud)

或者,您可以在switch语句中执行此操作(我不喜欢这样,但您可以):

public String getNameForReel(int reelValue) {
    switch(reelValue) {
       case 0: return "Cherry";
       case 1: return "Bell";
       case 2: return "Lemon";
       case 3: return "Bar";
       case 4: return "Seven";
    }
}
Run Code Online (Sandbox Code Playgroud)