我遇到了抛硬币模拟的问题。 我已经和java一起工作了几个小时,所以请耐心等待。 我希望程序做的是显示标题一次,给出菜单(按T ....)运行随机模拟并循环回菜单,除非玩家按Q.一切正常,因为我正在单独测试每个添加。随机生成器,打印,但你可能会告诉我,我不知道如何实现KeyListener。 我如何使其成功?
import java.util.Random;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class CoinToss
{
public void menu implements KeyListener
{
addKeyListener(this);
System.out.println("*******************************************************");
System.out.println("**********************WELCOME**************************");
System.out.println("*******************TO COIN TOSS************************");
System.out.println("*******************************************************");
}
@Override
public void keyPressed(KeyEvent e)
{
boolean gameOn = true;
while (gameOn == true)
{
Random rand = new Random();
System.out.println("PRESS T TO TOSS A COIN \nPRESS Q TO QUIT");
switch(e.getKeyCode())
{
case KeyEvent.VK_T:
int n = rand.nextInt(2);
if(n == 0)
{
System.out.println("HEADS");
}
if(n == 1)
{
System.out.println("TAILS");
}
if(n == 2)
{
gameOn = true;
}
break;
case KeyEvent.VK_Q:
System.out.println("GOOD BYE! \nSEE YOU NEXT TIME!");
gameOn = false;
break;
}
}
}
@Override
public void keyReleased(KeyEvent e)
{
}
@Override
public void keyTyped(KeyEvent e)
{
}
}