猜拳游戏BETA版

class Myexception extends Exception {
	public static void compute(int a) throws Myexception 
	{
		if(!(a==1 || a==2 || a==3))
		{
			 throw new Myexception();
		}
	}
}

/**
 * @(#)GuessFist.java
 *
 * GuessFist application
 *
 * @author kuroro
 * @version 1.00 2011/9/9
 */
 import java.util.Scanner;
public class GuessFist {
    
    public static void main(String[] args) {
    	int playernum1= 0;
        System.out.println("欢迎来到猜拳游戏");
       System.out.println("------------------------");
       do
       {	System.out.println("请选择一个角色进行游戏");
       		System.out.println("1.孙权");
       		System.out.println("2.刘备");
       		System.out.println("3.曹操");
       		System.out.println("请玩家输入所选角色对应的数字");
       	try{
       			Scanner enter = new Scanner(System.in);
       		     playernum1  = enter.nextInt();
       		    Myexception.compute(playernum1);
       	    	break;
       	}catch(Myexception ex){
       		System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
       	}catch(Exception ex){
       	
       			System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
        }
       }while(true);
         switch(playernum1)
       {
       	case 1: 
       		System.out.println("玩家选择的角色是孙权");break;
       	case 2:
       		 System.out.println("玩家选择的角色是刘备");break;
        case 3:
        	System.out.println("玩家选择的角色是曹操");break;
       	
       }
         System.out.println("------------------------");
         int computernum = (int)(Math.random()*3)+1;
          switch(computernum)
       {
       	case 1: 
       		System.out.println("电脑选择的角色是孙权");break;
       	case 2:
       		 System.out.println("电脑选择的角色是刘备");break;
        case 3:
        	System.out.println("电脑选择的角色是曹操");break;
       	
       }
       for(int i=0; i<=3;i++)
       {
        System.out.println("------------------------");
       	System.out.println("1.剪刀");
       	System.out.println("2.石头");
       	System.out.println("3.布");
       	int fist1 = 0;
       do{
       	 try{
       		  System.out.println("请玩家出拳(输入对应的数字)");
              fist1 = (new Scanner(System.in).nextInt());
              Myexception.compute(fist1);
       	      break;
        	}catch(Myexception ex){
       		    System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
        	}catch(Exception ex){
       	
       			System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
            }
        }while(true);
        String playerfist = fist1==1?"剪刀":fist1==2?"石头":"布";
        int fist2 = (int)(Math.random()*3)+1;
        String computerfist = fist2==1?"剪刀":fist2==2?"石头":"布";
         System.out.println("------------------------");
        System.out.println("玩家出的是"+playerfist);
        System.out.println("电脑出的是"+computerfist);
        if((fist1==1&&fist2==3)||(fist1==2&&fist2==1)||(fist1==3&&fist2==2))
        {
        	System.out.println("恭喜!本局玩家获胜");
        }
        else if((fist1==1&&fist2==1)||(fist1==2&&fist2==2)||(fist1==3&&fist2==3))
        {
           System.out.println("残念!本局双方握手言和");
        }else 
        {
        	 System.out.println("胜败乃兵家常事,大侠请重新来过!");
        }
          
       }
    }
}

修改了部分代码可以显示玩家选得角色名


 

/**
 * @(#)GuessFist.java
 *
 * GuessFist application
 *
 * @author kuroro
 * @version 1.00 2011/9/9
 */
 import java.util.Scanner;
public class GuessFist {
    
    public static void main(String[] args) {
    	int playernum1= 0;
    	String playername="";
    	String computername="";
        System.out.println("欢迎来到猜拳游戏");
       System.out.println("------------------------");
       do
       {	System.out.println("请选择一个角色进行游戏");
       		System.out.println("1.孙权");
       		System.out.println("2.刘备");
       		System.out.println("3.曹操");
       		System.out.println("请玩家输入所选角色对应的数字");
       	try{
       			Scanner enter = new Scanner(System.in);
       		     playernum1  = enter.nextInt();
       		    Myexception.compute(playernum1);
       	    	break;
       	}catch(Myexception ex){
       		System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
       	}catch(Exception ex){
       	
       			System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
        }
       }while(true);
         switch(playernum1)
       {
       	case 1: 
       		playername= "孙权";
       		System.out.println("玩家选择的角色是孙权");break;
       	case 2:
       		playername= "刘备";
       		System.out.println("玩家选择的角色是刘备");break;
        case 3:
        	playername= "曹操";
        	System.out.println("玩家选择的角色是曹操");break;
       	
       }
         System.out.println("------------------------");
         int computernum = (int)(Math.random()*3)+1;
          switch(computernum)
       {
       	case 1: 
       		computername= "孙权";
       		System.out.println("电脑选择的角色是孙权");break;
       	case 2:
       		computername= "刘备";
       		System.out.println("电脑选择的角色是刘备");break;
        case 3:
        	computername= "曹操";
        	System.out.println("电脑选择的角色是曹操");break;
       	
       }
       for(int i=0; i<=3;i++)
       {
        System.out.println("------------------------");
       	System.out.println("1.剪刀");
       	System.out.println("2.石头");
       	System.out.println("3.布");
       	int fist1 = 0;
       do{
       	 try{
       		  System.out.println("请"+playername+"出拳(输入对应的数字)");
              fist1 = (new Scanner(System.in).nextInt());
              Myexception.compute(fist1);
       	      break;
        	}catch(Myexception ex){
       		    System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
        	}catch(Exception ex){
       	
       			System.out.println("请从1~3中选择输入数字!!!!");
       			continue;
            }
        }while(true);
        String playerfist = fist1==1?"剪刀":fist1==2?"石头":"布";
        int fist2 = (int)(Math.random()*3)+1;
        String computerfist = fist2==1?"剪刀":fist2==2?"石头":"布";
         System.out.println("------------------------");
        System.out.println(playername+"出的是"+playerfist);
        System.out.println(computername+"出的是"+computerfist);
        if((fist1==1&&fist2==3)||(fist1==2&&fist2==1)||(fist1==3&&fist2==2))
        {
        	System.out.println("恭喜!本局玩家选的"+playername+"获胜");
        }
        else if((fist1==1&&fist2==1)||(fist1==2&&fist2==2)||(fist1==3&&fist2==3))
        {
           System.out.println("残念!"+playername+computername+"双方握手言和");
        }else 
        {
        	 System.out.println("玩家选择的"+playername+"输了T.T"+"胜败乃兵家常事,大侠请重新来过!");
        }
          
       }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值