Java猜拳游戏


总效果:
--------------------欢迎进入游戏世界----------------------
****************
** 猜拳,开始**
****************
出拳规则:1、剪刀 2、石头 3、布
请选择对方角色(1、刘备 2、孙权 3、曹操):3
请输入你的姓名:美丽
美丽 VS 曹操 对战

要开始吗?(y/n) y

请出拳:1、剪刀 2、石头 3、布(输入相应数字):2
你出拳:石头
曹操出拳:剪刀
结果:恭喜,你赢了!

是否开始下一轮?(y/n) n
曹操 VS 王子
对战次数:1

姓名 得分
王子 1
曹操 0

恭喜恭喜!

一、gui类(界面)

package FingerGuessingGame;

public class gui {
	public void Gui() {
		System.out.println("------欢迎进入游戏世界------");
		System.out.println("*********************");
		System.out.println("**  猜拳,开始  ** ");
		System.out.println("*********************");
		System.out.println("出拳规则:1、剪刀 2、石头 3、布");
	}
}

二、person类(用户类)

package FingerGuessingGame;

import java.util.Scanner;

public class person {
	Scanner sc = new Scanner(System.in);
	String pname = "";
	String pfist = "";

	public String name() {
		System.out.print("请输入您的姓名:");
		pname = sc.next();
		return pname;
	}

	public String fist() {
		System.out.print("请出拳:1、剪刀 2、石头 3、布(输入相应数字):");
		int a = sc.nextInt();
		switch (a) {
		case 1:
			pfist = "剪刀";
			break;
		case 2:
			pfist = "石头";
			break;
		case 3:
			pfist = "布";
			break;
		default:
			System.out.println("***************************");
			System.out.println("输入错误,请重新输入:");
			break;
		}
		return pfist;
	}

}

三、computer类(计算机类)

package FingerGuessingGame;

import java.util.Scanner;

public class computer {
	Scanner sc = new Scanner(System.in);
	String cname = "";
	String cfist = "";

	public String name() {
		System.out.print("请选择对方角色(1、刘备 2、孙权 3、曹操):");
		int a = sc.nextInt();
		switch (a) {
		case 1:
			cname = "刘备";
			break;
		case 2:
			cname = "孙权";
			break;
		case 3:
			cname = "曹操";
			break;
		default:
			System.out.println("***************************");
			System.out.println("输入错误,请重新输入:");
			break;
		}
		return cname;
	}

	public String fist() {
		int a = (int) (Math.random() * 3 + 1);
		switch (a) {
		case 1:
			cfist = "剪刀";
			break;
		case 2:
			cfist = "石头";
			break;
		case 3:
			cfist = "布";
			break;
		default:
			System.out.println("***************************");
			System.out.println("输入错误,请重新输入:");
			break;
		}
		return cfist;
	}

}

四、referee类(裁判类)

package FingerGuessingGame;

import java.util.Scanner;

public class referee {
	Scanner sc = new Scanner(System.in);
	person p = new person();
	computer c = new computer();
	gui g = new gui();
	int count = 0;
	int cintegral = 0;
	int pintegral = 0;

	public void rgui() {
		g.Gui();
		c.name();
		p.name();
		System.out.println(p.pname + "VS" + c.cname);
	}

	public void judge() {
		if (p.pfist.equals("剪刀") && c.cfist.equals("石头") || p.pfist.equals("石头") && c.cfist.equals("布")
				|| p.pfist.equals("布") && c.cfist.equals("剪刀")) {
			System.out.println("对不起,您输给了" + c.cname);
			cintegral++;
		} else if (p.pfist.equals(c.cfist)) {
			System.out.println("您与" + c.cname + "平局!");
		} else {
			System.out.println("恭喜,您赢了" + c.cname);
			pintegral++;
		}
	}

	public void StartGame() {
		System.out.print("您是否要开始?(y/n):");
		String a = sc.next();
		switch (a) {
		case "y":
			p.fist();
			System.out.println("您出拳:" + p.pfist);
			c.fist();
			System.out.println(c.cname + "出拳:" + c.cfist);
			judge();
			count++;
			System.out.print("您是否要开始下一局?(y/n):");
			String b = sc.next();
			System.out.println("--------------------------");
			switch (b) {
			case "y":
				StartGame();
				break;
			case "n":
				System.out.println("游戏结束!!");
				break;
			}
		case "n":
			break;
		}
	}

	public void GameOver() {
		System.out.println("--------------------------");
		System.out.println("您总共玩了" + count + "局!您的得分情况如下:");
		System.out.println("姓名\t得分");
		System.out.println(p.pname + "\t" + pintegral);
		System.out.println(c.cname + "\t" + cintegral);
	}
}

五、game类

package FingerGuessingGame;

public class game {
	public static void main(String[] args) {
		referee r=new referee();
		r.rgui();
		r.StartGame();
		r.GameOver();
	}
}

六、效果展示

在这里插入图片描述

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java程序设计》课程实验指导书程序代码(答案)(实验一),个人原创,仅供参考与交流。 实验一 Java开发环境的安装与配置,熟悉Java程序结构 一、实验目的: 1. 掌握JDK的安装步骤。 2. 理解环境变量PATH, CLASSPATH的作用,以及它们的设置方法。 3. 熟悉Editplus(或TextPad,JCreator)编辑环境,编写简单的Application程序和Applet程序,并编译和执行。 二、实验内容: 熟悉JDK的安装和配置,学习如何编写并运行简单的Application程序和Applet程序(能输出一条简单的问候信息); 三、实验要求: 1. 能正确地安装JDK 2. 熟悉环境变量PATH, CLASSPATH设置方法,熟悉编辑环境 3. 调试程序、编译,运行后得到正确的结果 4. 写出实验报告,要求记录编译和执行Java程序当中的系统错误信息提示,并给出解决办法。 四、实验步骤: 1.从http://java.sun.com 上下载最新版本的JDK,并安装。 2.设置环境变量PATH, CLASSPATH, 使得Java程序能正确编译和执行。 3.在Editplus(或JCreator ,Textpad)环境下编写一个HelloWorld.java程序, (1)在主方法static public void main(String[ ] args)中调用System.out.println()方法,使程序输出一条问候信息; (2) 编译运行程序,观察运行情况和输出结果。(使用JDK环境,调用javac.exe和java.exe编译和执行程序) 4.在Editplus(或JCreator ,Textpad)环境下编写一个HelloWorldApplet.java程序。 (1)在public void paint(Graphics g)方法中调用g.drawString()方法,使程序输出一条问候信息; (2) 编译运行程序,观察运行情况和输出结果。(使用JDK环境,调用javac.exe编译和浏览器解释执行Applet程序)
好的,以下是一个简单的 Java 猜拳游戏的示例代码: ```java import java.util.Scanner; public class RockPaperScissors { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] choices = {"rock", "paper", "scissors"}; int computerChoice = (int) (Math.random() * 3); // 电脑随机出拳 System.out.print("Please choose rock, paper, or scissors: "); String userChoice = scanner.next().toLowerCase(); while (!userChoice.equals("rock") && !userChoice.equals("paper") && !userChoice.equals("scissors")) { System.out.print("Invalid input. Please choose rock, paper, or scissors: "); userChoice = scanner.next().toLowerCase(); } System.out.println("You chose " + userChoice + "."); System.out.println("The computer chose " + choices[computerChoice] + "."); if (userChoice.equals(choices[computerChoice])) { System.out.println("It's a tie!"); } else if ((userChoice.equals("rock") && choices[computerChoice].equals("scissors")) || (userChoice.equals("paper") && choices[computerChoice].equals("rock")) || (userChoice.equals("scissors") && choices[computerChoice].equals("paper"))) { System.out.println("You win!"); } else { System.out.println("You lose!"); } scanner.close(); } } ``` 代码中,首先定义了三个选择项:rock、paper 和 scissors。接着,使用 `Math.random()` 方法随机生成电脑的出拳结果。然后,通过 `Scanner` 获取用户输入的选择,如果输入值不合法,则提示重新输入。最后,通过比较用户和电脑的出拳结果来判断胜负,并输出相应的结果。 注意,这只是一个简单的示例代码,实际应用中可能需要加入更多的逻辑和交互设计。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Juvenile少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值