我的 CS 课有作业问题。我应该做一台老虎机,但我无法继续工作,我知道扫描仪 nextLine 方法会跳过该行并保留那里的内容,但它不会让我输入任何内容,它只会结束程序。由于我的教授要求的格式,我还必须将此方法拆分并使其不超过 30 行。我还必须保持奖金的总和,但我不知道如何为头奖做这件事。
/**
* ProgrammingAssignment2.java
*
* @author Corey Goff
* @version March 2013
*/
import java.util.Random;
import java.util.Scanner;
/**
* This class simulates a slot machine.
*/
public class SlotMachine
{
/**
* This is the main method.
*
* @param args
*/
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
Random random = new Random();
String cont = "n";
char answer;
int coin = 0;
int totalEntered = 0;
int a;
int b;
int c;
int n;
int amountWon = 0;
int dubs = coin * 2;
int trips = coin * 4;
while (cont.equals("n"))
{
a = random.nextInt(6);
b = random.nextInt(6);
c = random.nextInt(6);
n = random.nextInt(991) +10;
totalEntered += coin;
System.out.println("How much would you like to bet? ");
coin = keyboard.nextInt();
switch (a)
{
case 0:
System.out.println("Cherry");
break;
case 1:
System.out.println("Orange");
break;
case 2:
System.out.println("Plum");
break;
case 3:
System.out.println("Bell");
break;
case 4:
System.out.println("Melon");
break;
default:
System.out.println("Bar");
}
switch (b)
{
case 0:
System.out.println("Cherry");
break;
case 1:
System.out.println("Orange");
break;
case 2:
System.out.println("Plum");
break;
case 3:
System.out.println("Bell");
break;
case 4:
System.out.println("Melon");
break;
default:
System.out.println("Bar");
}
switch (c)
{
case 0:
System.out.println("Cherry");
break;
case 1:
System.out.println("Orange");
break;
case 2:
System.out.println("Plum");
break;
case 3:
System.out.println("Bell");
break;
case 4:
System.out.println("Melon");
break;
default:
System.out.println("Bar");
}
if (a != b && a != c && b != c)
{
System.out.println("You have won $0");
}
else if (a == b || a == c || b == c)
{
System.out.println("Congratulations, you have won $" + dubs);
amountWon += dubs;
}
else if (a == b && a == c && a != 0)
{
System.out.println("Congratulations, you have won $" + trips);
amountWon += trips;
}
else if (a == 0 && b == 0 && c == 0)
{
System.out.println("Congratulations! You have won the jackpot of $"
+ (coin * n));
}
System.out.println("Continue? y/n ");
cont = keyboard.nextLine();
}
}
}