我遇到了CS課的作業問題。我應該製作一臺老虎機,但我無法繼續工作,我得到掃描儀nextLine方法跳過線,並保持那裏有什麼,但它不會讓我輸入任何東西,它只是結束程序。由於我的教授需要格式化,所以我也必須將這種方法分開,並將其設置爲30行或更少。我也必須保持贏得獎金總額,但我無法弄清楚如何爲大獎做。Java老虎機
/**
* 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();
}
}
}
你爲什麼全部投票? Atlest讓OP知道他如何改進這個問題。 – Smit 2013-03-27 18:55:05