1、用户类(Person)
(1)属性
①姓名(name)
②积分(score)
(2)方法
①出拳(showFist())
显示如下:
请出拳,1、剪刀 2、石头 3、布(请输入相应数字):2
你出拳:石头
2、计算机类(Computer)
(1)属性
①姓名(name)
②积分(score)
(2)方法
①出拳(showFist())
产生一个1~3的随机数,模拟计算机出拳结果。
显示如下:
曹操出拳:剪刀
3、游戏类(Game)
(1)属性
①用户
②计算机
③对战次数
(2)方法
①初始化(init())
创建用户对象,计算机对象,对战次数=0
②开始游戏
1)实现对战对方的选择
------------------------欢迎进入游戏世界---------------------
** 猜拳,开始 **
出拳规则:1、剪刀 2、石头 3、布
请选择对方角色(1、刘备 2、孙权 3、曹操):3
你选择了曹操对战
2)双方出拳
3)计算胜负
4)实现循环对战
5)统计对战次数
6)统计各方得分情况
import java.util.Scanner;
public class Person {
Scanner sc = new Scanner(System.in);
String name;
int score;
public int showFist() {
System.out.println("请出拳,1、剪刀 2、石头 3、布(请输入相应的数字):");
int num = sc.nextInt();
switch (num) {
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
}
return num;
}
}
import java.util.Scanner;
public class Computer {
Scanner sc = new Scanner(System.in);
String name;
int score;
public int showFist() {
int max = 4;
int min = 1;
int num = (int) (Math.random() * (max - min)) + min;// (Math.random())范围是[0,1)
switch (num) {
case 1:
System.out.println(this.name + " 出:剪刀");
break;
case 2:
System.out.println(this.name + " 出:石头");
break;
case 3:
System.out.println(this.name + " 出:布");
break;
}
return num;
}
}
import java.util.Scanner;
public class Game {
Scanner sc = new Scanner(System.in);
Person person;
Computer computer;
// Person p = new Person();
// Computer c = new Computer();
int num;
public void show() {
Person p = new Person();
Computer c = new Computer();
this.person = p;
this.computer = c;
System.out.println("————————欢迎进入游戏世界————————");
System.out.println("—————————————————————————————");
System.out.println("———————————猜拳,开始——————————");
System.out.println("—————————————————————————————");
System.out.println("出拳规则:1、剪刀 2、石头 3、布");
System.out.println("请选择对方的角色(1、刘备 2、孙权 3、曹操)");
int num1 = sc.nextInt();
switch (num1) {
case 1:
computer.name = "刘备";
System.out.println("你选择了刘备对战");
break;
case 2:
computer.name = "孙权";
System.out.println("你选择了孙权对战");
break;
case 3:
computer.name = "曹操";
System.out.println("你选择了曹操对战");
break;
}
// return num1;
}
public void show1() {
show();
num = 0;
while (true) {
int nump = person.showFist();
int numc = computer.showFist();
if ((nump == 1) && (numc == 3)) {
System.out.println("恭喜,你赢了!");
person.score++;
} else if ((numc == 1) && (nump == 3)) {
System.out.println("很可惜,你输了,加油!");
computer.score++;
} else if (nump > numc) {
System.out.println("恭喜,你赢了!");
person.score++;
} else if (numc > nump) {
System.out.println("很可惜,你输了,加油!");
computer.score++;
} else {
System.out.println("可惜,平局,双方都不加分");
}
System.out.println("继续?(y/n)");
String a = sc.next();
if (a.equals("y")) {
num++;
} else {
break;
}
}
System.out.println("对战次数" + (1 + num));
System.out.println("我方得分:" + person.score);
System.out.println("对方得分:" + computer.score);
}
}
public class H1 {
public static void main(String[] args) {
Game g = new Game();
g.show1();
//测试
// g.show();
// Person p=new Person();
// p.showFist();
// System.out.println(p.showFist());
// Computer c=new Computer();
// System.out.println(c.showFist());
// c.showFist();
}
}



