问题及代码:
package first;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class text {
public static void main(String[] args) {
int option = JOptionPane.YES_OPTION;
while (option == JOptionPane.YES_OPTION) {
System.out.println("scissor for 0 , rock for 1 , paper for 2");
String n[] = { "scissor", "rock", "paper" };
Scanner get = new Scanner(System.in);
int a, b;
int i = 0, j = 0;
while (true) {
int cp = (int) (Math.random() * 3);
a = get.nextInt();
while (!(a == 0 || a == 1 || a == 2)) {
System.out.println("输入非法,请重新输入!");
a = get.nextInt();
}
if (a - cp == 1)
++i;
else if (cp == a)
;
else if (cp == 2 && a == 0)
++i;
else
++j;
System.out.println("you is " + n[a] + " the cp is " + n[cp]);
b = i;
if ((i > 1) || (j > 1))
break;
}
if (b > 1)
System.out.println("you win!");
else
System.out.println("the cp win!");
option = JOptionPane.showConfirmDialog(null, "Continue ? ");
}
}
}
运行结果: