我正在对一台老虎机做一个简单的模拟,经过一段时间的工作后,我已经让它在大多数情况下工作了。唯一的问题是当我要求用户输入他们想要下注多少钱时,程序只有在他们按两次输入时才会进行。我不知道造成这种情况的原因是什么,我觉得这是一个非常简单的解决方案,所以任何可能的帮助都会非常感激!
以下是该计划:
import java.util.*;
import java.text.DecimalFormat;
public class SlotMachineSimulation
{
public static void main(String[] args)
{
Random randomN = new Random();
DecimalFormat money = new DecimalFormat("$###,###,###,##0.00");
Scanner kb = new Scanner(System.in);
String fruit1 = "abcd";
String fruit2 = "abcd";
String fruit3 = "abcd";
int slot1 = 0;
int slot2 = 0;
int slot3 = 0;
double finalMoney = 0;
double factor = 0;
String aaa = "y";
while(aaa.equalsIgnoreCase("y"))
{
System.out.print("Enter the amount you would like to bet: ");
int bet = kb.nextInt();
factor = 0;
for (int i = 1; i <= 3; i++)
{
final int maxRandom = 6;
int slot = randomN.nextInt(maxRandom);
int iteration = i;
if(iteration == 1)
{
slot1 = slot;
switch (slot)
{
case 0:
fruit1 = "cherries";
break;
case 1:
fruit1 = "oranges";
break;
case 2:
fruit1 = "plums";
break;
case 3:
fruit1 = "bells";
break;
case 4:
fruit1 = "melons";
break;
case 5:
fruit1 = "bars";
break;
}
}
else if(iteration == 2)
{
slot2 = slot;
switch(slot)
{
case 0:
fruit2 = "cherries";
break;
case 1:
fruit2 = "oranges";
break;
case 2:
fruit2 = "plums";
break;
case 3:
fruit2 = "bells";
break;
case 4:
fruit2 = "melons";
break;
case 5:
fruit2 = "bars";
break;
}
}
else if(iteration == 3)
{
slot3 = slot;
switch(slot)
{
case 0:
fruit3 = "cherries";
break;
case 1:
fruit3 = "oranges";
break;
case 2:
fruit3 = "plums";
break;
case 3:
fruit3 = "bells";
break;
case 4:
fruit3 = "melons";
break;
case 5:
fruit3 = "bars";
break;
}
System.out.println("-" + fruit1 + "--" + fruit2 + "--" + fruit3
+ "-");
if(slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
{
if( slot1 == slot2 && slot1 == slot3)
{
System.out.println("Great! Three Match!");
System.out.println("That triples your bet!");
factor = 3;
}
else
{
System.out.println("Great! Two Match!");
System.out.println("That doubles your bet!");
factor = 2;
}
}
else
System.out.println("Sorry, none match...");
double rawWinnings = factor*bet;
System.out.println("You win " + money.format(rawWinnings));
if(rawWinnings>0)
finalMoney = finalMoney + rawWinnings;
else
finalMoney = finalMoney + bet;
System.out.print("Would you like to play again(enter y or n): ");
}
aaa = kb.nextLine();
if(aaa.equalsIgnoreCase("n"))
{
System.out.println("You won a total of " + finalMoney);
System.exit(0);
}
}
}
}
}
答案 0 :(得分:1)
如@ajb所述,问题来自指令aaa = kb.nextLine()
。但是,让我进一步了解一些细节,以便您可以准确了解发生了什么以及为什么。
在控制台中键入一个整数然后按ENTER
:Java会保存您键入的内容,包括&#34;行尾#34;字符(a.k.a&#34; CR&#34;或&#34;回车&#34;),即与ENTER
键关联的字符。
所以,想象你按下按键&#34; 1&#34; +&#34; 2&#34; +&#34; ENTER&#34;,Java将您键入的内容保存为12\n
(其中\n
只是缓冲区中&#34;回车符号的表示。
kb.nextInt()
仅从缓冲区中读取12
并离开\n
。
接下来,您进入主while
循环并在内部for
循环中迭代 3次。在内部kb.nextLine()
循环的每次迭代中调用for
。在第一次迭代上,您不会显示任何内容,但kb.nextLine()
会读取缓冲区中剩余的\n
。在第二次迭代中,您仍然不显示任何内容,但缓冲区现在为空,这导致aaa.nextLine()
等待新行,您再按一次{{1 }}。以下是ENTER
和nextInt()
之间的主要区别:nextLine()
不读取nextInt()
,但是\n
。最后,在第三次迭代中,一切都恢复正常。
为了帮助您理解,以下是您的代码的简化版本:
nextLine()
现在,尝试我修改的这个版本的下一个版本,以便它可以工作,并看到差异:
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String aaa = "y";
while(aaa.equalsIgnoreCase("y"))
{
System.out.println("Bet ? : ");
int bet = kb.nextInt();
for (int i = 1; i <= 3; i++)
{
if (i == 3)
{
System.out.print("Would you like to play again(enter y or n): ");
}
System.out.println("I read a line !");
aaa = kb.nextLine();
if (aaa.equalsIgnoreCase("n"))
{
System.out.println("Goodbye !");
System.exit(0);
}
}
}
}
希望这会有所帮助:)