你可以使用消息对话框。我知道我曾经制作了一个完全依赖于它们运行的程序。
这是一个石头剪刀布游戏程序,我之前做的程序完全基于选项面板运行。
import java.util.Random;
import javax.swing.JOptionPane;
public class AI
{
public static void main(String[] args)
{
Random generator = new Random();
int scan = 0;
String bool = "";
int rps;
String type = "";
int option;
int counter = 0;
String stuff = "";
int cpu = 0;
int foo = 0;
int total = 0;
while (true)
{
while(true)
{
type = JOptionPane.showInputDialog("Choose what game you would like to play. \n best of 3 \n best of 5 \n best of 7 \n first to 10");
stuff = type.toLowerCase();
if (stuff.equals ("best of 3"))
{
option = JOptionPane.showConfirmDialog(null, "Play a best of 3 game?", "Exit", JOptionPane.YES_NO_OPTION);
break;
}
else if (stuff.equals ("best of 5"))
{
option = JOptionPane.showConfirmDialog(null, "Play a best of 5 game?", "Exit", JOptionPane.YES_NO_OPTION);
break;
}
else if (stuff.equals ("best of 7"))
{
option = JOptionPane.showConfirmDialog(null, "Play a best of 7 game?", "Exit", JOptionPane.YES_NO_OPTION);
break;
}
else if (stuff.equals ("first to 10"))
{
option = JOptionPane.showConfirmDialog(null, "Play a game to 10?", "Exit", JOptionPane.YES_NO_OPTION);
break;
}
else
{
continue;
}
}
if (stuff.equals("best of 3"))
foo = 3;
else if (stuff.equals("best of 5"))
foo = 5;
else if (stuff.equals("best of 7"))
foo = 7;
else if (stuff.equals("first to 10"))
foo = 10;
for (int i = 0; i <= foo; i++)
{
while(true)
{
scan = Integer.parseInt(JOptionPane.showInputDialog ("Enter a number between 1 and 3. \n 1: rock\n 2: paper\n 3: scissors"));
if (scan == 1)
{
JOptionPane.showMessageDialog(null,"You chose rock.");
break;
}
else if (scan == 2)
{
JOptionPane.showMessageDialog(null,"You chose paper.");
break;
}
else if (scan == 3)
{
JOptionPane.showMessageDialog(null,"You chose scissors.");
break;
}
else
{
JOptionPane.showMessageDialog(null,"Choose a valid number");
continue;
}
}
rps = generator.nextInt(3) + 1;
String str;
if (rps == 1)
{
str = "The cpu chose rock!";
}
else if (rps == 2)
{
str = "The cpu chose paper!";
}
else
{
str = "The cpu chose scissors!";
}
JOptionPane.showMessageDialog(null,str);
if (rps == 1 && scan == 2)
{
JOptionPane.showMessageDialog(null,"you won!");
counter++;
total++;
}
else if (rps == 2 && scan == 3)
{
JOptionPane.showMessageDialog(null,"you won!");
counter++;
total++;
}
else if (rps == 3 && scan == 1)
{
JOptionPane.showMessageDialog(null,"you won!");
counter++;
total++;
}
else if (rps == scan)
{
JOptionPane.showMessageDialog(null,"Try again!");
continue;
}
else
{
JOptionPane.showMessageDialog(null,"you lose!");
cpu++;
total++;
}
if(stuff.equals("best of 3") && counter == 2 || cpu ==2)
{
break;
}
else if (stuff.equals("best of 5") && counter==3 || cpu == 3)
{
break;
}
else if (stuff.equals("best of 7") && counter == 4 || cpu == 4)
{
break;
}
else if (stuff.equals("first to 10") && counter == 10 || cpu ==10)
{
break;
}
}
bool = JOptionPane.showInputDialog ("do you want play another game? \n Type in yes or no");
if (bool.equals("yes"))
continue;
else if (bool.equals("no"))
break;
}
}
}