java-石头剪刀布

写Java的一个石头剪刀布游戏

package work;

import java.util.Scanner;

public class Work4 {
//石头剪刀布游戏,r=石头,s=剪刀,p=布
	
	public static int judge(char a,char b) {
		if(a=='r')
		{
			if(b=='r')
				return 0;
			else if(b=='s')
				return 1;
			else
				return -1;
		}
		else if(a=='s')
		{
			if(b=='s')
				return 0;
			else if(b=='p')
				return 1;
			else
				return -1;
		}
		else
		{
			if(b=='p')
				return 0;
			else if(b=='r')
				return 1;
			else
				return -1;
		}
	}
	public static void main(String[] args) {
		try (Scanner in = new Scanner(System.in)) {
			System.out.println("游戏说明:r=石头,s=剪刀,p=布");
			System.out.println("请选择模式"+"\n"+"人机模式(请输入0),人人模式(请输入1)");
			int temp=in.nextInt();
			
			char people;//人出的手势
			int computer_round=(int)(1+Math.random()*3);//随机数来生成电脑的手势
			int judge_win;//判断那方胜利
			
			if(temp==0)//人机模式
			{
				char computer;
				if(computer_round==1)
					computer='r';
				else if(computer_round==2)
					computer='s';
				else
					computer='p';
				
				System.out.print("我方回合,请出:");
				people=in.next().charAt(0);
				System.out.print("\n");
				System.out.println("电脑回合,电脑出了"+computer);
				if(people=='r' || people=='s' || people=='p')
					judge_win=judge(people,computer);
				else
					judge_win=2;
			}
			else if(temp==1)//人人模式
			{
				char people2;
				System.out.print("我方回合,请出:");
				people=in.next().charAt(0);
				System.out.print("\n");
				System.out.print("对手回合,请出:");
				people2=in.next().charAt(0);
				if( (people=='r' || people=='s' || people=='p')&&( people2=='r' || people2=='s' || people2=='p') )
					judge_win=judge(people,people2);
				else
					judge_win=2;
			}
			else//输入错误
			{
				judge_win=2;
				System.out.println("输入错误!");
			}
			
			if(temp==1 || temp==0)//在是回合的情况下
			{
				if(judge_win==0)
					System.out.println("平局!");
				else if(judge_win==1)
					System.out.println("我方胜利!");
				else if(judge_win==-1)
				{
					if(temp==1)
						System.out.println("对手胜利!");
					else
						System.out.println("电脑胜利!");
				}
				else
					System.out.println("你或者是你的对手出的手势有误!");
			}
			
		}
	}

}

有啥可以优化的欢迎指出!

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值