人机猜拳
import java.util.Scanner;
public class Person {
Scanner input = new Scanner(System.in);
String name;
int score;
String action;
int num;
public void method() {
System.out.println("\n请出拳:1.剪刀 2.石头 3.布");
boolean a = true;
do {
num = input.nextInt();
if (num == 1 || num == 2 || num == 3) {
switch (num) {
case 1:
action = "剪刀";
break;
case 2:
action = "石头";
break;
case 3:
action = "布";
break;
}
a = false;
System.out.println("你出拳:" + action);
} else {
System.out.println("输入数字有误,请重新输入");
}
} while (a);
}
}
第二个类,电脑类:
public class Computer {
String name;
int score;
String action;
int num;
public void method(){
num=(int)((Math.random())*3)+1;
switch(num){
case 1:
action="剪刀";
break;
case 2:
action="石头";
break;
case 3:
action="布";
break;
}
System.out.println(name+"出拳:"+action);
}
}
第三个类,游戏类:
import java.util.Scanner;
public class Game {
Scanner input = new Scanner(System.in);
Person person = new Person();//创建玩家类的对象
Computer computer = new Computer();//创建电脑类的对象
int number;
int frequency = 0;
public void process() {
System.out
.println("--------------------------欢迎进入游戏世界--------------------------\n");
System.out.println("\t\t********************************");
System.out.println("\t\t**********猜拳,开始*************");
System.out.println("\t\t********************************");
System.out.println();
System.out.println("出拳规则:1.剪刀 2.石头 3.布");
System.out.print("请选择对方角色(1:刘备2:孙权3:曹操):");
boolean b = true;
do {
number = input.nextInt();
if (number == 1 || number == 2 || number == 3) {
switch (number) {
case 1:
computer.name = "刘备";
break;
case 2:
computer.name = "孙权";
break;
case 3:
computer.name = "曹操";
break;
}
b = false;
} else {
System.out.println("输入数字有误,请重新输入");
}
} while (b);
System.out.print("请输入你的姓名:");
person.name = input.next();
System.out.println(person.name + " VS " + computer.name + " 对战\n");
System.out.println("要开始吗?(y/n)");
char answer = input.next().charAt(0);
while (answer == 'y') {
person.method();
computer.method();
if (person.num == computer.num) {
System.out.println("嘿嘿,和局,等着瞧吧!");
} else if (((person.num == 2) && (computer.num == 1))
|| (person.num == 1) && (computer.num == 3)
|| ((person.num == 3) && (computer.num == 2))) {
System.out.println("哇,你赢了,好厉害!");
person.score++;
} else {
System.out.println("^_^!!!你输了,真笨!");
computer.score++;
}
frequency++;
System.out.println("\n");
System.out.println("还要继续吗?(y/n)");
answer = input.next().charAt(0);
}
}
public void showResult() {
System.out.println("********************************");
System.out.println(person.name + " VS " + computer.name);
System.out.println("对战次数:" + frequency);
System.out.println("\n姓名\t\t得分");
System.out.println(person.name + "\t\t" + person.score);
System.out.println(computer.name + "\t\t" + computer.score);
if (person.score < computer.score) {
System.out.println("呵呵,笨笨,下次加油!");
} else if (person.score == computer.score) {
System.out.println("哇,竟然平局,我们下次一决胜负!");
} else {
System.out.println("哇,你好棒啊!");
}
System.out.println("********************************");
}
}
测试类:
public class Demo {
public static void main(String[] args) {
Game play=new Game();//创建游戏类的对象
play.process();//调用游戏类的游戏进程方法
play.showResult();//调用游戏类的结算方法
}
}