用Java对象类知识实现剪刀石头布游戏

 闲着没事干做了一个蹩脚的剪刀石头布

package project;

import java.util.Scanner;
import java.util.Random;

class Scissors{		//定义类Scissors
	boolean Computer;
	boolean People;
}

class Rock{		//定义类Stone
	boolean Computer;
	boolean People;
}

class Paper{		//定义类Cloth
	boolean Computer;
	boolean People;
}

class Guess {
	public static void main (String args[]) {
		Scissors a1 = new Scissors();		//引用成员变量
		Rock a2 = new Rock();
		Paper a3 = new Paper();
		
		String result = null;
		String People = null;
		String Computer = null;
		
		int P1 = 0;
		int C1 = 0;
		int Max = 4;//定义随机数最大值
		int Min = 1;//定义随机数最小值
		
		
		while(true) {		//判定玩家输入
		System.out.println("Rock paper scissors(1 for scissors, 2 for rock, and 3 for paper)");
		Scanner scan = new Scanner(System.in);//输入
		String Ps1 = scan.nextLine();
		while(true) {
		try {
			P1=Integer.parseInt(Ps1);//转换为int类型
			break;
			}
		catch(Exception e){
			System.out.println("Please enter a number.");//强转失败,继续输入
			Ps1 = scan.nextLine();
			}
		}
		
		
		Character.toString(P1);//取首字符
		
		
		if (P1<4 && P1>0) {//判定是否大于0小于4
			break;		
		}else {
			System.out.println("Please enter a number.(4>number>0)");//重新输入
		}
		
		
		}
		while(true) {
		Random random = new Random();
		C1 = (int) Min + (int) (Math.random() * (Max - Min));//获取机器输入
		if (C1<4 && C1>0) {//判定是否大于0小于4
			break;	
		}
		}
		
		
		switch(P1) {//玩家变量
		case 1:
			a1.People = true;
			People = "Scissors";
			break;
		case 2:
			a2.People = true;
			People = "Rock";
			break;
		case 3:
			a3.People = true;
			People = "Paper";
			break;
		}
		
		
		switch(C1){//电脑变量
		case 1:
			a1.Computer = true;
			Computer = "Scissors";
			break;
		case 2:
			a2.Computer = true;
			Computer = "Rock";
			break;
		case 3:
			a3.Computer = true;
			Computer = "Paper";
			break;
		}
		
		
		//判定输赢
		if (a1.Computer==true&&a2.People==true || a2.Computer==true&&a3.People==true || a3.People==true&&a1.Computer==true) {
			result = "win.";
		}else if(a1.Computer==true&&a1.People==true||a2.Computer==true&&a2.People==true||a3.Computer==true&&a3.People==true){
			result = "drew";
		}
		else {
			result = "lose.";
		}
		
		
		System.out.println("You are"+" "+People+","+"Computer is"+" "+Computer+".");//输出玩家和电脑的结果
		System.out.println("You are"+" "+result);//输出输赢
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值