书本上JAVA人机猜拳代码

这个是我在书上自己抄着来试试看的,自己写话,估计没这个能力

不过有个问题,要是用户输入的不是1-3的数字而是其他的,有没有其他办法能让额外的数字也转化为1-3啊?

import java.util.Scanner;

 abstract class Player{
	String name;
	int score;
	
	
	public Player(){
		
		name="匿名";
		score=0;
	}
	
	public abstract int showFist();
	
}


//User

 class Person extends Player{
	public int showFist(){
		Scanner input=new Scanner(System.in);
		System.out.print("\n请出拳: 1剪刀 2石头 3布 (输入相应数字) :");
		int show=input.nextInt();
		switch(show){
			case 1:
				System.out.println("你出拳:剪刀");
				break;
			case 2:
				System.out.println("你出拳:石头");
				break;
			case 3:
				System.out.println("你出拳:布");
				break;
		}
		return show;
	}
	
}

//Computer

 class Computer extends Player{
	public int showFist(){
		int show=(int)(Math.random()*10)%3+1;
		switch(show){
			case 1:
				System.out.println(name+"出拳:剪刀");
				break;
			case 2:
				System.out.println(name+"出拳:石头");
				break;
			case 3:
				System.out.println(name+"出拳:布");
				break;
		}
		return show;
		
	}
	
}


// Game
 class Game{
	Person person;
	Computer computer;
	int count;                                              //对战次数
	
	//初始化
	public void initial(){
		person=new Person();
		computer=new Computer();
		count=0;
	}
	
	//开始游戏
	public void startGame(){
		System.out.println("-----------欢迎进入游戏世界-------------\n");
		System.out.println("\n\t\t*****************");
		System.out.println("\t\t猜拳,开始  **");
		System.out.println("\t\t********************");
		
		System.out.println("\n\n出拳规则:1剪刀 2石头 3布");
		/*选择对方角色*/
		System.out.print("请选择角色(1龙珠 2悟空 3BB):");
		Scanner input=new Scanner(System.in);
		int role=input.nextInt();
		if(role==1){
			computer.name="龙珠";
		}else if(role==2){
			computer.name="悟空";
		}else if(role==3){
			computer.name="BB";
		}
		
		System.out.print("\n 要开始吗?(y/n)");
		String con=input.next();
		int perFist;   //用户出拳
		int compFist;  //计算机出拳
		
		while(con.equals("y")){
			perFist=person.showFist();
			compFist=computer.showFist();
			
			//裁决
			if((perFist==1&&compFist==1)||
				(perFist==2&&compFist==2)||
				(perFist==3&&compFist==3)){
					System.out.println("结果:和局,再来一局吧!\n");
			}else if((perFist==1 && compFist==3)||
					  (perFist==2 && compFist==1)||
					  (perFist==3 && compFist== 2)){
						  System.out.println("结果:恭喜,你赢了!");
						  person.score++;
					  }else{
						System.out.println("结果:你输了!");
							computer.score++;
						}
			count++;
			System.out.print("\n是否开始下一轮(y/n):");
			con=input.next();
					  
		}
		showResult();
	}
	
	
//显示比赛结果
  
	public void showResult(){
		//显示最后结果
		System.out.println("--------------------------------------------");
		System.out.println(computer.name+"VS"+person.name);
		System.out.println("对战次数:"+count);
		int result=calcResult();
		if(result==1){
			System.out.println("结果:打开平手");
		}else if(result==2){
			System.out.println("结果:恭喜");
		}else{
			System.out.println("结果:你输了!下局再来");
		}
		System.out.println("--------------------------------------------");
		
	}
	
//计算比赛结果
	public int calcResult(){
		if(person.score==computer.score){
			return 1;
		}else if(person.score>computer.score){
			return 2;
		}else{
			return 3;
		}
		
	}
	
	
}

public class Demo{
	public static void main(String args[]){
		Game game=new Game();
		game.initial();
		game.startGame();
	}
}

还在学习中,所以以上的代码都是写在Demo.java文件中,暂时还不会分开不同java文件来写.....

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值