人机猜拳
功能 选取对战角色猜拳记录分数
public class User {
public String name;
public int score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public void init(){
System.out.println("\t");
System.out.println("\t 猜拳, 开始 ");
System.out.println("\t");
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
}
public int showFist(int choice){
if (choice==1) {
System.out.println("你出拳:"+"剪刀");
}else if(choice ==2 ){
System.out.println("你出拳:"+"石头");
}else if(choice == 3){
System.out.println("你出拳:"+"布");
}else {
System.out.println("输入不合法");
}
return choice;
}
}
import java.util.Random;
public class Computer {
public int showFist(){
Random rd = new Random();
int i = rd.nextInt(3)+1;
if(i1){
System.out.println("电脑出拳:"+"剪刀");
}
if(i2){
System.out.println("电脑出拳:"+"石头");
}
if(i==3){
System.out.println("电脑出拳:"+"布");
}
return i;
}
}
import java.util.Scanner;
public class TestGuess {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
User user = new User();
Computer cmp = new Computer();
int count1=0,count2=0,count3=0;
String str="y";
user.init();
String[] inits={"刘备","孙权","曹操"};
System.out.print("请选择对方角色(1.刘备 2.孙权 3.曹操):");
int role =sc.nextInt();
System.out.print("请输入你的姓名:");
String name;
name = sc.next();
System.out.println(name+"\tVS\t"+inits[role-1]+"\t对战");
System.out.print("要开始吗?()y/n");
str=sc.next();
do {
System.out.print("请出拳:1.剪刀 2.石头 3.布 (请输入相应数字):");
int choice = sc.nextInt();
user.showFist(choice);
int j=cmp.showFist();
if ((choice==1&&j==3)||(choice==2&&j==1)||(choice==3&&j==2)){
System.out.println("你赢了");
count1++;
}else if(choice==j){
System.out.println("结果是:平局");
count3++;
}else{
System.out.println("电脑赢了");
count2++;
}
System.out.print("是否开始下一轮?(y/n)");
str=sc.next();
} while (str.equals("y"));
if(str.equals("n")){
System.out.println("姓名\t"+"得分");
System.out.println(name+"\t"+count1);
System.out.println(inits[role-1]+"\t"+count2);
System.out.println("对战次数:"+(count1+count2+count3));
if(count1>count2){
System.out.println("结果:笨笨电脑,我赢了");
}else if(count1==count2){
System.out.println("结果:平局下次再和你一绝高下");
}else {
System.out.println("结果:呵呵,我竟然输了");
}
}
}
}