问题
思路分析(有助于你们去分析如何写这类问题)
运行结果演示
演示1:
演示2:
package 剪子布暴捶;
/*
姓名:马志勇
许昌学院
互注 互助 互祝
有问题联系QQ:3089065946
*/
import
java.util.Scanner;
abstract class Player {
String name;
int grade;
public abstract void show();
}
class ComputerPlayer
extends Player{
private String name="AlphaGo";
private int grade;
public ComputerPlayer() {
this.show();
}
public void show() {
//出拳
double dou=((Math.random()*3)+1)*1000;
int in=(int)dou/1000;
if(in==0) {
this.grade=2;
}else {
this.grade=in;
}
}
public int getGrade() {
return grade;
}
public String getName() {
return name;
}
}
class PersonPlayer extends Player{
private String name;
private int grade;
public PersonPlayer(String name) {
this.name=name;
this.show();
}
public void show() {
Scanner sc=new Scanner(System.in);
System.out.print("请输入你的技能:");
int value=sc.nextInt();
for(int i=0;;i++) {
if(value3) {
System.out.println("输入有误!请重新输入:");
value=sc.nextInt();
}else {
break;
}
}
this.grade=value;
}
public int getGrade() {
return grade;
}
public String getName() {
return name;
}
}
class Game{
private ComputerPlayer computer;
private PersonPlayer player;
public Game(String name) {
computer=new ComputerPlayer();
player=new PersonPlayer(name);
start();
}
public void start() {
/*
1.剪子 2.布 3.锤
*/
int com=computer.getGrade();
int ply=player.getGrade();
String comname=computer.getName();
String plyname=player.getName();
System.out.println("电脑玩家:"+comname+"技能:"+com);
System.out.println("人类玩家:"+plyname+"技能:"+ply);
System.out.println("--------------------------------");
if(com==1) {//电脑是剪子
if(ply==1) {
System.out.println("平局");
}else if(ply==2) {
System.out.println(comname+"获胜");
}else {
System.out.println(plyname+"获胜");
}
}else if(com==2) {//电脑是布
if(ply==1) {
System.out.println(plyname+"获胜");
}else if(ply==2) {
System.out.println("平局");
}else {
System.out.println(comname+"获胜");
}
}else if(com==3) {//电脑是锤
if(ply==1) {
System.out.println(comname+"获胜");
}else if(ply==2) {
System.out.println(plyname+"获胜");
}else {
System.out.println("平局");
}
}
}
}
public class Test{
public static void main(String[] args) {
System.out.println("剪子布包捶有序加载中...");
Game g=new Game("杨林");
}
}