源代码:
public class computer extends caicai{
String b;
caicai c=new caicai();
public computer() {
int i=(int) (Math.random()*3);
if(i==0){
System.out.println("电脑:拳头");
b="拳头";
}
else if(i==1) {
System.out.println("电脑:剪刀");
b="剪刀";
}
else {
System.out.println("电脑:布");
b="布";
}
}
public void computer() {
if(c.a.equals(b))
System.out.println("平局");
else if(c.a.equals("拳头")&&b.equals("剪刀")||c.a.equals("剪刀")&&b.equals("布")||c.a.equals("布")&&b.equals("拳头")){
System.out.println("赢了");
}
else
System.out.println("输了");
}
}
public class caicai {
static String a;
public void caicai() {
System.out.println("请猜拳:");
Scanner in=new Scanner(System.in);
this.a=in.nextLine(); }
}
public class ceshi {
public static void main(String[] args) {
for(;;) {
caicai wan=new caicai();
wan.caicai();
computer pc=new computer();
pc.computer();
}
}
}
结果示例: 吾独矣 终极愿望世界和平
|