无法弄清楚为什么我的代码只会在我需要一个字符串时返回一个 Int 并且帮助会很棒。下面的代码。我尝试将变量声明为 String 但没有运气。
我想返回 3 个随机字符串:cherry、grape、bell 或 x
import java.util.Scanner;
import java.util.Random;
public class slot {
public static void main(String[] args)
{
String answer = "y";
int cherry;
int grape;
int bell;
int x;
Random generator = new Random(); // random generator
Scanner scan = new Scanner (System.in); // scanner class
System.out.println("Would you like to play the slot machine?(y/n): ");
answer = scan.nextLine();
while(answer.equalsIgnoreCase("y"))
{
cherry = generator.nextInt(5); // generates a random number
grape = generator.nextInt(5);
bell = generator.nextInt(5);
System.out.println("The three numbers of the slot machine are: " + cherry +grape +bell);
if(cherry == grape && grape == bell)
{
System.out.println("JACKPOT! All three of the same");
}
if(cherry == grape || cherry == bell || grape == bell )
{
System.out.println("Close, you got two of the same!!");
}
else
{
System.out.println("Not a winner");
}
System.out.print("Try again?(y/n): ");
answer = scan.nextLine();
System.out.println();
}
System.out.println("Bye!!");
}
}