Java基础案例教程2-5剪刀石头布

时间:2025-04-13 09:15:01
package chapter;
import ;
import ;
public class example2_5 {
	public static void main(String[] args){
		Scanner input=new Scanner();
		int win=0;
		int loss=0;
		int draw=0;
		for(int i=1;i<=5;i++) {
			("当前为第"+i+"局");
			int randomNumber=new Random().nextInt(3);
			("请选择出剪刀、石头、布");
			String a=();
			if(("剪刀")) {
				if(randomNumber==0) {
					("电脑为:剪刀");
					("平局!");
					draw++;	
				}
				else if(randomNumber==1) {
					("电脑为:石头");
					("失败!");
					loss++;
				}
				else if(randomNumber==2) {
					("电脑为:布");
					("胜出");
					win++;
				}
			}
			else if(("石头")) {
				if(randomNumber==0) {
					("电脑为:剪刀");
					("胜出");
					win++;
				}
				else if(randomNumber==1) {
					("电脑为:石头");
					("平局");
					draw++;	
				}
				else if(randomNumber==2) {
					("电脑为:布");
					("失败");
					loss++;
				}
			}
			else if(("布")) {
				if(randomNumber==0) {
					("电脑为:剪刀");
					("失败");
					loss++;
				}
				else if(randomNumber==1) {
					("电脑为:石头");
					("胜出");
					win++;
				}
				else if(randomNumber==2) {
					("电脑为:布");
					("平局");
					draw++;
					
				}
			}
			else {
				("输入有误!");
			}
		}
		("游戏结束!五局游戏中一共赢"+win+"局,失败"+loss+"局,平"+draw+"局");
		if(win>loss) {
			("胜出");
		}
		else if(loss<win) {
			("失败");
		}
		else {
			("平局");
		}
	}

}