闲着没事干做了一个蹩脚的剪刀石头布
package project;
import java.util.Scanner;
import java.util.Random;
class Scissors{ //定义类Scissors
boolean Computer;
boolean People;
}
class Rock{ //定义类Stone
boolean Computer;
boolean People;
}
class Paper{ //定义类Cloth
boolean Computer;
boolean People;
}
class Guess {
public static void main (String args[]) {
Scissors a1 = new Scissors(); //引用成员变量
Rock a2 = new Rock();
Paper a3 = new Paper();
String result = null;
String People = null;
String Computer = null;
int P1 = 0;
int C1 = 0;
int Max = 4;//定义随机数最大值
int Min = 1;//定义随机数最小值
while(true) { //判定玩家输入
System.out.println("Rock paper scissors(1 for scissors, 2 for rock, and 3 for paper)");
Scanner scan = new Scanner(System.in);//输入
String Ps1 = scan.nextLine();
while(true) {
try {
P1=Integer.parseInt(Ps1);//转换为int类型
break;
}
catch(Exception e){
System.out.println("Please enter a number.");//强转失败,继续输入
Ps1 = scan.nextLine();
}
}
Character.toString(P1);//取首字符
if (P1<4 && P1>0) {//判定是否大于0小于4
break;
}else {
System.out.println("Please enter a number.(4>number>0)");//重新输入
}
}
while(true) {
Random random = new Random();
C1 = (int) Min + (int) (Math.random() * (Max - Min));//获取机器输入
if (C1<4 && C1>0) {//判定是否大于0小于4
break;
}
}
switch(P1) {//玩家变量
case 1:
a1.People = true;
People = "Scissors";
break;
case 2:
a2.People = true;
People = "Rock";
break;
case 3:
a3.People = true;
People = "Paper";
break;
}
switch(C1){//电脑变量
case 1:
a1.Computer = true;
Computer = "Scissors";
break;
case 2:
a2.Computer = true;
Computer = "Rock";
break;
case 3:
a3.Computer = true;
Computer = "Paper";
break;
}
//判定输赢
if (a1.Computer==true&&a2.People==true || a2.Computer==true&&a3.People==true || a3.People==true&&a1.Computer==true) {
result = "win.";
}else if(a1.Computer==true&&a1.People==true||a2.Computer==true&&a2.People==true||a3.Computer==true&&a3.People==true){
result = "drew";
}
else {
result = "lose.";
}
System.out.println("You are"+" "+People+","+"Computer is"+" "+Computer+".");//输出玩家和电脑的结果
System.out.println("You are"+" "+result);//输出输赢
}
}