嗨,我正在尝试完成我的高中AP CS班的学期期末项目。我们应该用Java制作3个赌场游戏。我唯一遇到麻烦的是掷骰子。具体来说,我有一个扫描仪,玩家可以输入要下注通过或不通过的信息。我有一个单独的扫描仪,因此他们可以输入有多少“钱”,这可以工作,但第二个却不行。当我按下Enter键时,什么也没发生。
我的代码:
import java.util.Scanner;
import java.util.Random;
public class Craps
{
public static void main(String[] args)
{
Craps();
}
public static void Craps()
{
int die1;
int die2;
int roll;
int point;
Scanner cash = new Scanner(System.in);
Scanner scan = new Scanner(System.in);
System.out.println("Hello Player. How much cash ya got?");
String cashcash = new String(cash.next());
String input = new String("");
int money = Integer.parseInt(cashcash);
while (money > 0)
{
System.out.println("What do you wanna bet? Pass or Don't Pass?");
input = scan.next();
die1 = randomInt(1, 7);
die2 = randomInt(1, 7);
roll = die1 + die2;
if (roll == 2 || roll == 3 || roll == 12)
{
if (input.startsWith("d") || input.startsWith("D"))
{
money += 1;
System.out.println(roll);
System.out.println("You win! You have $"+money);
}
else if (input.startsWith("p") || input.startsWith("P"))
{
money -= 1;
System.out.println(roll);
System.out.println("You lose! You have $"+money);
}
}
else if (roll == 7 || roll == 11)
{
if (input.startsWith("d") || input.startsWith("D"))
{
money -= 1;
System.out.println(roll);
System.out.println("You lose! You have $"+money);
}
else if (input.startsWith("p") || input.startsWith("P"))
{
money += 1;
System.out.println(roll);
System.out.println("You win! You have $"+money);
}
}
else if (roll == 4 || roll == 5 || roll == 6 || roll == 8 || roll == 9 || roll == 10)
{
point = roll;
die1 = randomInt(1, 7);
die2 = randomInt(1, 7);
roll = die1+die2;
while (!(roll == point) || !(roll == 7))
{
die1 = randomInt(1, 7);
die2 = randomInt(1, 7);
roll = die1+die2;
}
if (roll == point)
{
if (input.startsWith("d") || input.startsWith("D"))
{
money -= 1;
System.out.println(roll);
System.out.println("You lose! You have $"+money);
}
else if (input.startsWith("p") || input.startsWith("P"))
{
money += 1;
System.out.println(roll);
System.out.println("You win! You have $"+money);
}
}
else if (roll == 7)
{
if (input.startsWith("d") || input.startsWith("D"))
{
money += 1;
System.out.println(roll);
System.out.println("You win! You have $"+money);
}
else if (input.startsWith("p") || input.startsWith("P"))
{
money -= 1;
System.out.println(roll);
System.out.println("You lose! You have $"+money);
}
}
}
}
}
public static int randomInt(int l, int h)
{
Random r = new Random();
int result = r.nextInt(h-l) + l;
return result;
}
}
(我知道我的代码中可能有很多事情不是最佳的,但这是明天要交的,我只需要知道如何使扫描仪工作即可。谢谢!:)