java人机猜拳-石头剪刀布
- 格式:docx
- 大小:19.80 KB
- 文档页数:4
JAVA⼩游戏代码(剪⼑⽯头布) /** 创建⼀个类Game,⽯头,剪⼑,布的游戏。
*/public class Game {/*** @param args*/String[] s ={"⽯头","剪⼑","布"};//获取电脑出拳String getComputer(int i){String computerGuess = s[i];return computerGuess;}//判断⼈出拳是否为⽯头,剪⼑,布boolean isOrder(String guess){boolean b = false;for(int x = 0;x < s.length; x++){if(guess.equals(s[x])){b = true;break;}}return b;}//⽐较void winOrLose(String guess1,String guess2){if(guess1.equals(guess2)){System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。
平了");}else if(guess1.equals("⽯头")){if(guess2.equals("剪⼑"))System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。
You Win!");}else{System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。
You Lose!");}}else if(guess1.equals("剪⼑")){if(guess2.equals("布")){System.out.println("你出:" + guess1 + ",电脑出:" + guess2 + "。
java⾯向对象编程--猜拳⼩游戏java⾯向对象编程实现--猜拳⼩游戏⽬标⽬标:玩家在控制台和电脑猜拳,电脑每次都会随机出⽯头/剪⼑/布,直到玩家选择退出游戏。
记录并显⽰玩家和电脑猜拳的成绩。
设计思路分析电脑的随机猜拳可以使⽤随机数⽣成,这⾥规定 0表⽰⽯头,1 表⽰剪⼑,2 表⽰布。
为了显⽰清晰,可以设置⼀个⽅法将⽣成的随机数转换为对应⽯头/剪⼑/布。
玩家在控制台输⼊(⽯头/剪⼑/布),但玩家也可能输⼊别的数,所以这⾥需要做⼀个玩家的输⼊校验 ,并考虑给玩家退出游戏的选择。
记录的结果有玩家猜拳选择,电脑猜拳选择和胜负。
为了⽅便管理和显⽰,这⾥设计⼀个结果类。
⽤于记录猜拳的结果,因为猜拳次数可能不⽌⼀次,所以考虑将结果保存到集合中,这⾥使⽤ ArrayList集合。
具体代码实现如下:import java.util.ArrayList;import java.util.Scanner;public class FingerGuessingGame {//测试实现类public static void main(String[] args) {Tom tom = new Tom();tom.guess();}}//玩家类class Tom {Scanner sc = new Scanner(System.in);//猜拳public void guess() {System.out.println("----------猜拳游戏开始(-1退出)---------");//使⽤ArrayList保存结果ArrayList<GuessResult> results = new ArrayList<>();while (true) {//玩家输⼊String tomGuess = checkInput();//如果输⼊-1退出游戏if (tomGuess.equals("-1"))break;//⽣成0-2的随机数int num = (int) (Math.random() * 3);//将获取到的数字按照之前的规定转换为字符串String comGuess = convertComputerGuess(num);System.out.println("电脑出 " + comGuess);//判断输赢String isWin = winORLoose(tomGuess, comGuess);System.out.println(isWin);//将结果添加到集合中results.add(new GuessResult(tomGuess, comGuess, isWin));}//输出结果System.out.println("-------本次猜拳的结果------");System.out.println("玩家\t\t\t电脑\t\t\t胜负");for (GuessResult result : results) {System.out.println(result);}}//获取电脑猜拳结果public String convertComputerGuess(int num) {//0代表⽯头,1剪⼑,2布if (num == 0)return "⽯头";if (num == 1)return "剪⼑";if (num == 2)return "布";return "";}//玩家输⼊校验public String checkInput() {while (true) {System.out.println("你出(⽯头/剪⼑/布)-1退出:");String choice = sc.next();if (choice.equals("⽯头") || choice.equals("剪⼑") ||choice.equals("布") || choice.equals("-1")) {return choice;} elseSystem.out.println("你的输⼊有误! 请检查并重新输⼊:");}}//判断输赢public String winORLoose(String tomGuess, String comGuess) {if (tomGuess.equals("⽯头") && comGuess.equals("剪⼑"))return "赢";else if (tomGuess.equals("剪⼑") && comGuess.equals("布"))return "赢";else if (tomGuess.equals("布") && comGuess.equals("⽯头"))return "赢";else if (tomGuess.equals(comGuess))return "平⼿";elsereturn "输";}}//结果类⽤于记录猜拳的结果class GuessResult {private String tomGuess;private String ComGuess;private String isWin;public GuessResult(String tomGuess, String comGuess, String isWin) { this.tomGuess = tomGuess;ComGuess = comGuess;this.isWin = isWin;}@Overridepublic String toString() {returntomGuess +"\t\t\t"+ComGuess + "\t\t\t" +isWin ;}}。
大连科技学院Java课程设计报告题目剪刀石头布人机对战系统的设计与实现学生姓名李易霖专业班级计算机10-1指导教师李孝贵史原职称讲师所在单位信息科学系软件工程教研室教学部主任王立娟完成日期 2013年9月13日课程设计报告单综合评定:(优、良、中、及格、不及格)指导教师签字:2013年9月16日《Java课程设计》任务书任务及要求:一、设计(研究)内容和要求内容:编写一个管理软件,使用GUI界面,实现信息管理功能。
包括对信息的查询,添加,删除等操作。
采用数据库存储数据。
要求:1、软件要求(1)实现界面。
进行系统需求分析和系统设计,写出系统分析和设计报告。
(2)实现数据库的连接和应用功能。
(3)实现对数据的查询,添加,删除等功能。
2、文档要求(1) 设计程序结构、画出模块图。
(2)按照指导教师给出的项目文档的模板进行书写。
(3) 参考文献数量不得低于4个。
二、工作量2周(10个工作日)时间三、计划安排第1个工作日-第2个工作日:查找相关资料、书籍,阅读示例文档;设计程序结构、模块图、完成类的设计。
第3个工作日-第8个工作日:完成程序的编码,并且自己调试、测试。
穿插进行项目文档的撰写。
第9个工作日:撰写和整理项目文档。
第10个工作日:由教师检查软件测试效果、项目文档,给出学生本课程成绩。
指导教师签字:2013年9月2日目录题目: (1)1.需求分析 (1)2.概要设计 (1)3.详细设计 (1)4.编码 (3)5.调试分析 (5)6.测试结果及运行效果 (6)7.系统开发所用到的技术 (8)参考文献 (9)附录全部代码 (10)题目:石头剪刀布人机对战系统的设计与实现1.需求分析这是一个古老耐玩的游戏,根据一般的常识,首先要有两个人,两个人同时出,然后,根据双方的出拳,决定谁是赢者,虽然想起来这个游戏很简单,但如何实现,还是有一定难度的。
实现这个时针时涉及到几个技术问题:1.双方如何出拳2.如何同时出拳3.如何判断哪方赢了4.如何显示结果剪刀石头布游戏,都是通过两个人同时出拳,然后根据双方出的拳,判定哪方是赢家。
package meng;import java.awt.Color;import java.awt.Font;import java.awt.Rectangle;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JTextArea;public class abc extends JFrame implements ActionListener {JPanel panel; //panel面板JButton toneButton; //石头按钮JButton jianButton; //剪刀按钮JButton buButton; //布按钮JButton clearButton; //清除按钮JLabel label; //显示分数标签JTextArea textArea; //显示结果文本区int win=0; //赢了int fail=0; //输了int ping=0; //平了int grade=0; //分数计数int i=1; //1表示剪刀,2标示石头,3表示布String[] set=new String[]{null,"剪刀","石头","布"}; //存放剪刀石头布String result=" "; //显示分数的字符串public abc() //构造函数{//实例化各个控件和设置各个控件的在面板里的位置panel=new JPanel();panel.setLayout(null);panel.setBackground(Color.yellow);textArea=new JTextArea();textArea.setBounds(new Rectangle(0,50,400,100));textArea.setEditable(false);textArea.setBackground(Color.yellow);textArea.setFont(new Font("宋体",Font.BOLD,30));toneButton=new JButton("石头");toneButton.setBounds(new Rectangle(0,0,100,50)); jianButton=new JButton("剪刀");jianButton.setBounds(new Rectangle(100,0,100,50)); buButton=new JButton("布");buButton.setBounds(new Rectangle(200,0,100,50)); clearButton=new JButton("清空");clearButton.setBounds(new Rectangle(300,0,100,50));buButton.setBackground(Color.green); jianButton.setBackground(Color.green); toneButton.setBackground(Color.green); clearButton.setBackground(Color.green);label=new JLabel(result);label.setBounds(new Rectangle(0,150,300,60));//实现按钮监听toneButton.addActionListener(this);jianButton.addActionListener(this);buButton.addActionListener(this);clearButton.addActionListener(this);//向panel面板添加各个控件panel.add(textArea);panel.add(toneButton);panel.add(jianButton);panel.add(buButton);panel.add(clearButton);panel.add(label);//将面板panel添加到JFrame里add(panel);setBounds(490,280,400,250);setVisible(true);setTitle("张三");setDefaultCloseOperation(3);}public static void main(String[] args) //主方法{abc p=new abc();}public void actionPerformed(ActionEvent e) //实现按钮的单击事件{if(e.getSource()==toneButton) //单击石头按钮{i=2;computeResult();}if(e.getSource()==jianButton){i=1;computeResult();}if(e.getSource()==buButton){i=3;computeResult();}if(e.getSource()==clearButton){win=0;fail=0;ping=0;grade=0;textArea.setText("");label.setText("");// setResult();}}public void computeResult() //计算你出拳和电脑出拳的结果{int j=(int)(Math.random()*3+1); //电脑随机产生数字1,2,3 String comStr=null; //保存计算机出的拳String playStr=null; //保存玩家出的拳for(int a=1;a<set.length;a++){if(a==j){comStr=set[a];}if(a==i){playStr=set[a];}}if(i-j==-2||i-j==1) //玩家赢得时候{win++;grade++;textArea.setText("你出:"+playStr+"\n电脑出:"+comStr+"\n结果:你赢了");setResult();}else if(i-j==0) //电脑和玩家打平的时候{ping++;textArea.setText("你出:"+playStr+"\n电脑出:"+comStr+"\n结果:平局");setResult();}else //电脑获胜或你输了的时候{fail++;textArea.setText("你出:"+playStr+"\n电脑出:"+comStr+"\n结果:你输了");setResult();}}public void setResult() //用与更新分数标签的结果{result=" 分数: 赢: "+ win + " 平: "+ ping + " 输: "+ fail + " 得分: "+grade;label.setText(result);}}。
Java剪⼑⽯头布package day09_test;import java.util.Random;import java.util.Scanner;import day09.GamePlayer;import day09.GameRobot;public class GameTest {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("请输⼊猜拳⽐赛回合数:");int maxNumber = input.nextInt();//回合计数器int count = 0;//玩家胜利数int countP = 0;//电脑胜利数int countR = 0;while(count<maxNumber) {System.out.println("请输⼊你想出的数字:(1.剪⼑ 2.⽯头 3.布)");int PlayNum = input.nextInt();if (PlayNum>3 || PlayNum<=0) {System.out.println("输⼊有误!请重新输⼊:");PlayNum =input.nextInt();}GamePlayer player = new GamePlayer(PlayNum);player.say();//机器⼈随机输⼊1-3Random r = new Random();int RobotNum = r.nextInt(3)+1;GameRobot robot = new GameRobot(RobotNum);robot.say();//判断输赢if (player.getNumber()==robot.getNumber()) {count++;System.out.println("第"+count+"局:双⽅⼀致,请重新猜拳!");}else if (player.getNumber()==1) {//玩家出剪⼑if (robot.getNumber()==3) {//电脑出布count++;countP++;System.out.println("第"+count+"局:玩家赢,您得⼀分!");}else if (robot.getNumber()==2) {//电脑出⽯头count++;countR++;System.out.println("第"+count+"局:电脑赢,它得⼀分!");}}else if (player.getNumber()==2) {//玩家出⽯头if (robot.getNumber()==3) {//电脑出布count++;countP++;System.out.println("第"+count+"局:电脑赢,它得⼀分!");}else if (robot.getNumber()==1) {//电脑出剪⼑count++;countR++;System.out.println("第"+count+"局:玩家赢,您得⼀分!");}else if (player.getNumber()==3) {//玩家出布if (robot.getNumber()==2) {//电脑出布count++;countP++;System.out.println("第"+count+"局:玩家赢,您得⼀分!");}else if (robot.getNumber()==1) {//电脑出剪⼑count++;countR++;System.out.println("第"+count+"局:电脑赢,它得⼀分!");}}}if (countP==countR) {System.out.println("电脑得分:"+countR+" 玩家得分:"+countP); System.out.println("⽐赛结束,平局!");}else if (countP>countR) {System.out.println("电脑得分:"+countR+" 玩家得分:"+countP); System.out.println("⽐赛结束,玩家胜利!");}else {System.out.println("电脑得分:"+countR+" 玩家得分:"+countP); System.out.println("⽐赛结束,电脑胜利!");}}}package day09;public class GameRobot {private int number;public int getNumber() {return number;}public void setNumber(int number) {this.number = number;}public GameRobot() {super();// TODO Auto-generated constructor stub}public GameRobot(int number) {super();this.number = number;}public void say() {switch(this.number) {case 1:System.out.println("电脑出的是剪⼑");break;case 2:System.out.println("电脑出的是⽯头");break;case 3:System.out.println("电脑出的是布");break;}}package day09;/*** 今天的任务是通过控制台⽅式实现⼀个⼈机对战的猜拳游戏,⽤户通过输⼊(1.剪⼑ 2.⽯头 3.布),机器随机⽣成(1.剪⼑ 2.⽯头 3.布),胜者积分,n 局以后通过积分的多少判定胜负。
JAVA人机猜拳小游戏代码//JAVA人机猜拳小游戏代码 import java.util.Scanner; public class guess{public static void main(String[] args){Scanner in=new Scanner(System.in);System.out.println("-----猜拳游戏-------"); System.out.println("请出拳(1.剪刀 2.石头 3.布)"); int person=in.nextInt();int computer=(int)(Math.random()*3)+1;String Marks="拳头";//这是给人做一个标记;String Marks2="拳头";//这是给电脑做一个标记;switch(person){case 1:Marks="拳头";break;case 2:Marks="石头";break;case 3:Marks="布";break;}switch(computer){case 1:Marks2="拳头";break;case 2:Marks2="石头";break;case 3:Marks2="布";break;}if(person==computer){System.out.println("你出的是:"+Marks+"电脑出的是:"+Marks2+"---平局");}elseif(person==1&&computer==2||person==2&&computer==3||person==3&&computer==1){System.out.println("你出的是:"+Marks+"电脑出的是:"+Marks2+"—_—你输了~");}else{System.out.println("你出的是:"+Marks+"电脑出的是:"+Marks2+"^_^你赢了~");}}}//应用说明:只要把代码复制到TXT写字板再把扩展名改为JAVA就行了;。
2012届课程设计20《剪刀、石头、布小游戏》课程设计说明书学生姓名代燕清学号5042209016所属学院信息工程学院专业计算机网络班级计算机网络专12指导教师李旭教师职称讲师塔里木大学教务处制我做的“剪刀,石头,布”游戏是一个很简单的Java小游戏,这个游戏是进行人机对战的,玩这个游戏时你只需要在对话框中输入自己想的答案就好了,例如你输入的是“石头”然后确认,这是电脑就会随即给出“剪刀,石头,布”中的一个,然后电脑会根据游戏规则判断对错问题。
用户只需要,输入心中想的答案即可。
关键词:Java;猜数字摘要 (1)目录 (2)前言 (2)项目概况 (3)2.1项目所用的时间 (3)2.2项目负责人 (4)2.3项目指导人 (4)正文 (4)3.1设计分析 (4)3.2程序结构(流程图) (4)3.3操作方法 (4)3.4试验结果(包括输入数据和输出结果) (5)总结 (6)致谢 (7)参考文献 (8)附录 (9)前言Java是在网络时代诞生的,因此必须适应网络发展的特殊需要.Java的发展和壮大并且逐渐成为网络变成的主流语言,则充分说明了java适应了网络发展的特殊需要,学习好该门课程是成为一个好的java程序员的前提条件,通过此次课程设计使学生达到提高动手能力的目的.Java语言作为当今 INTERNET上最流行的编程语言,它的产生和WWW密切相关,所以课程中还将对WWW技术进行必要的介绍.同时,对于信息安全专业的学生,掌握Java中的安全包的API和Sandbox也是极其重要的.通过本课程的学习,使学生掌握网络编程的基本方法,能够根据现实生活实践编制出一些实用的客户机/服务器小程序.为进一步学习维护网站信息安全的建设打下基础.为了加深对JAVA语言的掌握及对面向对象程序设计基本思想的理解,提高对面向对象技术的具体应用,进行本次课程设计.此次我做的课程设计项目是要设计一个猜数字游戏。
这个题目都是比较基础的内容,是作为一个学习网络应当具备的能力。
java13⼈机猜拳public class Demo01 {public static void main(String[] args) {/** 你同桌和你要玩游戏.* 1 剪⼑,2 ⽯头,3 布*/// 判断结果.// 1 剪⼑,2 ⽯头,3 布int a = 1;// 剪⼑int b = 2;// ⽯头if (a == 1 && b == 3 || a == 2 && b == 1 || a == 3 && b == 2) {// 你赢了.System.out.println("你赢了");} else if (a == 1 && b == 2 || a == 2 && b == 3 || a == 3 && b == 1) {// 你输了System.out.println("你输了");} else {// 平局System.out.println("平局");}// int a = 5;// int b = 2;// 5⽐2⼤.}}package com.jh.test01;import java.util.Scanner;/**** ⽤户名* 属性: 姓名,积分.* 功能:出拳的功能.*/public class User {// 属性:// 姓名String name;// 积分--分数int score;// 出拳的功能。
/** 1 剪⼑,2 ⽯头,3 布* 1: 返回值类型。
int* 2:参数列表 -- ⽆*//*** "1 剪⼑,2 ⽯头,3 布"* @return 出的拳*/public int userHand() {Scanner sc = new Scanner(System.in);System.out.println("请输⼊你出的⼩拳拳");System.out.println("1 剪⼑,2 ⽯头,3 布");int num = sc.nextInt();// 等值判断switch (num) {case 1:System.out.println("您输出了剪⼑");break;case 2:System.out.println("您输出了⽯头");break;case 3:System.out.println("您输出了布");break;default:break;}return num;}}package com.jh.test01;import java.util.Random;/*** 电脑类。
Java实现⼈机对战猜拳游戏本⽂实例为⼤家分享了Java实现⼈机对战猜拳游戏的具体代码,供⼤家参考,具体内容如下通过控制台⽅式实现⼀个⼈机对战的猜拳游戏1.⽤户通过输⼊(2.剪⼑ 0.⽯头 5.布)2.机器随机⽣成(2.剪⼑ 0.⽯头 5.布)3.胜者积分4.n 局以后通过积分的多少判定胜负。
开发⼯具:IDEA分析:1.在这个猜拳游戏⾥⾸先要解决的是机器⼈如何出拳?解决:通过预设⼀个字符串,然后通过Random类的nextInt⽅法获取到⼀个随机整数,将这个整数作为字符串的下标,再通过循环的⽅法来组成⼀个随机数。
⽣成对应的出拳情况2.⽣成的随机数如何⽐较?解决:使⽤equals()进⾏⽐较3.如何积分?解决:先给⼈机各初始积分为0分,每⽐较⼀次就记⼀次分,这⾥赢⼀局记10分完整代码如下:import java.util.Random;import java.util.Scanner;public class MoraTest {public static void main(String[] args) {Scanner input = new Scanner(System.in);System.out.println("--- 猜拳游戏 ---");System.out.println("你想和机器⼈玩多少局:请输⼊(1-100)");//输⼊轮数int number = input.nextInt();System.out.println("请输⼊对应数值:\n0-⽯头 2-剪⼑ 5-布");int userIntegral = 0;//⽤户积分int robotIntegral = 0;//机器⼈积分for (int i = 0; i < number; i++) {String num = getRandom(1);//获取随机数String userNum = input.next();//输⼊出拳的值String u = putFist(userNum);//根据⽤户输⼊的值给对应的剪⼑、⽯头、布System.out.println("⽤户的出拳为:"+u);String n = putFist(num);//根据机器类随机⽣成的数值给对应的剪⼑、⽯头、布System.out.println("机器⼈出拳为:"+n);//如果⽤户出⽯头if ("0".equals(userNum)) {if ("2".equals(num)) {//如果机器⼈出剪⼑,⽤户获胜userIntegral += 10;System.out.println("⽤户获胜!积10分!");} else if ("5".equals(num)) {//如果机器出布,机器⼈获胜robotIntegral += 10;System.out.println("机器⼈获胜!积10分!");} else {//如果机器出⽯头,则平局,不积分System.out.println("平局!");}} else if ("2".equals(userNum)) {//如果⽤户出剪⼑if ("2".equals(num)) {//如果机器⼈也出剪⼑,则平局System.out.println("平局!");} else if ("5".equals(num)) {//如果机器出布,⽤户获胜userIntegral += 10;System.out.println("⽤户获胜!积10分!");} else {//如果机器出⽯头,机器⼈获胜robotIntegral += 10;System.out.println("机器⼈获胜!积10分!");}} else {//如果⽤户出布if ("2".equals(num)) {//如果机器⼈出剪⼑,机器⼈获胜robotIntegral += 10;System.out.println("机器⼈获胜!积10分!");} else if ("5".equals(num)) {//如果机器出布,则平局System.out.println("平局!");} else { //如果机器出⽯头,⽤户获胜userIntegral += 10;System.out.println("⽤户获胜!积10分!");}}num = null;}System.out.println("****************");System.out.println("战⽃结束,查看分数:");System.out.println("⽤户总积分:"+userIntegral+"分");System.out.println("机器⼈总积分:"+robotIntegral+"分");if (userIntegral > robotIntegral){System.out.println("经过"+number+ "局后,最终的胜利者是:⽤户!");} else if (userIntegral == robotIntegral) {System.out.println("经过"+number+ "局后,你们打成了平局");} else {System.out.println("经过"+number+ "局后,最终的胜利者是:机器⼈!");}}public static String putFist(String s){String fist = null;if ("0".equals(s)){fist = "拳头";} else if ("2".equals(s)) {fist = "剪⼑";} else if ("5".equals(s)){fist = "布";} else {System.err.println("你输⼊的不对!");return null;}return fist;}//拳头属性//0-⽯头 2-剪⼑ 5-布public static String getRandom(int length) {String fistNum = "520";//创建⼀个新的随机数⽣成器。
Java程序设计实验报告班级:11060341X 学号:50 姓名:马一桃实验题目:猜拳小游戏实验要求:用java编写一个人机对战的猜拳小游戏。
人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况。
有简单的操作界面。
实验内容:1、问题分析过程:人机猜拳:人可以通过选择出拳,通过键盘输入相应数字来实现。
电脑那么随机性出拳,由电脑产生随即数来实现。
通过游戏规那么判断双方的输赢,显示当局的输赢情况,并记录当局的分数,并各方的输赢情况。
游戏完毕时显示双方的分数以及输赢情况。
面向的对象有人、电脑以及游戏规那么。
人、电脑都有实现自己出拳的方法,同时还要有判断各方输赢的方法。
2、主要实现代码:〔要求有必要的注释〕:import java.util.Scanner; //通过import语句引入Scanner类public class Game{public static void main(String args[]){int x=0; //用户的初始分数为0int y=0; //电脑的初始分数为0int z=0; //开场时决战次数为0System.out.println("\n");System.out.println("猜拳游戏现在开场,准备好了吗?");System.out.println("===========游戏规那么===========");System.out.println(" 胜利加一分,否那么不加分 ");System.out.println(" 当游戏完毕时分高者为胜 ");System.out.println("==============================");Scanner shuzi = new Scanner(System.in); //用户通过键盘输入System.out.println("是否要开场游戏?"+"\n"+" y/n");String m = shuzi.next();while(m.equals("y")) {System.out.println("请选择数字出拳:");System.out.println("1 石头 2 剪刀 3 布");int A = shuzi.nextInt(); //利用switch语句,用户实现出拳switch(A){case 1:System.out.println("我出拳: 石头");break;case 2:System.out.println("我出拳: 剪刀");break;case 3:System.out.println("我出拳: 布");break;}int B = (int)((Math.random()*10)%3 + 1); //电脑产生随机数,进展出拳 switch(B){case 1:System.out.println("电脑出拳: 石头");break;case 2:System.out.println("电脑出拳: 剪刀");break;case 3:System.out.println("电脑出拳: 布");break;}if(A==B){ //一局游戏判断输赢的过程System.out.println("哎呦,平局!再努力! ");z++;}else if(((A==1)&(B!=3))||((A==2)&(B!=1))||((A==3)&(B!=2))){System.out.println("不错嘛,赢了!");x++;z++;}else{System.out.println("唉,输了,得加油啊!");y++;z++;}System.out.println("\n");System.out.println("有种再来一局"+"\n"+"y/n");m = shuzi.next();}System.out.println("游戏完毕" );System.out.println("对战的次数为"+z);System.out.println("你的分数:" +x);System.out.println("电脑的分数:"+y);if(x>y){ //最终比游戏结果的判断System.out.println("噢耶!完胜电脑!");}else if(x==y){System.out.println("彼此彼此,水平相当么~");}else{System.out.println("不幸输了,改日再和你比赛!");}}}开场界面进展一局游戏结果游戏最终结果的判断3、实验心得体会:3.1出现的问题及解决方案〔1〕在编译源文件时,出现了这么个问题这是一个编码的问题。
目录绪论............................................... 错误!未定义书签。
一、背景与意义..................................... 错误!未定义书签。
二、设计原理与方案................................. 错误!未定义书签。
一)方案....................................... 错误!未定义书签。
二)设计思路................................... 错误!未定义书签。
三)系统分析................................... 错误!未定义书签。
四)程序概要设计............................... 错误!未定义书签。
五)工作流程图................................ 错误!未定义书签。
六)程序框图.................................. 错误!未定义书签。
七)程序中所用类说明.......................... 错误!未定义书签。
八)程序运行效果及存在的问题................... 错误!未定义书签。
三、项目实现....................................... 错误!未定义书签。
一)程序用到的类库简介......................... 错误!未定义书签。
二)游戏主程序设计............................. 错误!未定义书签。
三)主界面PigPanel类......................... 错误!未定义书签。
四)具体实现.................................. 错误!未定义书签。
四、运行结果....................................... 错误!未定义书签。
Java程序设计实验报告班级:学号:姓名:实验题目:猜拳小游戏实验要求:用java编写一个人机对战的猜拳小游戏。
人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况。
有简单的操作界面。
实验内容:1、问题分析过程:(1)首先分析猜拳游戏本身的玩法:人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况。
(2)用面向对象的思想来分析:在游戏过程中有几个对象组成人电脑游戏规则抽象出3个类:Person、Computer、GamePerson类有哪些属性和行为呢?属性:名字name,输赢次数(比分)score行为:出拳ShowFirst()选择性Computer类有哪些属性和行为呢?属性:名字name,输赢次数(比分)score行为:出拳showFist()随机Game类有哪些属性和行为呢?属性:游戏的双方(person、computer)、对战局数count行为:产生角色initial()、游戏规则startGame()、显示比赛结果showResult()、统计比赛结果calcResul()2、主要实现代码:import java.util.*;public class StartGame {public static void main(String[]args){Game start = new Game();//实例化游戏类start.initial(); //调用初始化方法start.startGame(); //调用游戏开始方法start.showResult(); //调用游戏结果显示方法}}import java.util.*;public class Person {String name;//名字属性int score;//积分属性//出拳方法public int showFist(){System.out.print("\n请出拳:1.剪刀2.石头3.布(输入相应数字):");Scanner input = new Scanner(System.in);int num = input.nextInt();String fist = "";//保存出拳switch(num){case 1:fist = "剪刀";break;case 2:fist = "石头";break;case 3:fist = "布";break;}System.out.println(name + "出拳:" + fist);return num;}}import java.util.*;public class Game {//Person person; //甲方//Computer computer; //乙方int count; //对战次数Person person = new Person(); //实例化用户类Computer computer = new Computer(); //实例化计算机类//初始化方法public int initial(){count = 0;return count;}//游戏开始方法public void startGame(){//显示游戏开始界面System.out.println(" ---------------欢迎进入游戏世界--------------\n");System.out.println("\t\t******************************");System.out.println("\t\t** ^_^ 猜拳,Start ^_^ **");System.out.println("\t\t*****************************");System.out.println("\n\n出拳规则:1.剪刀2.石头3.布");//选择计算机角色System.out.print("请选择对方角色:1.刘备2.孙权3.曹操:");Scanner input = new Scanner(System.in);int num = input.nextInt();switch(num){case 1: = "刘备";break;case 2: = "孙权";break;case 3: = "曹操";break;}//输入用户角色名System.out.print("请输入你的姓名:"); = input.next(); //显示对战双方System.out.print( + " VS " + + " 对战\n");//开始游戏System.out.print("\n要开始吗?(y/n)");String con = input.next();int perFist;//用户出的拳int compFist;//计算机出的拳if(con.equals("y")){//判断是否开始String answer = "y";while("y".equals(answer)){//循环条件是是否开始下一轮//出拳perFist = person.showFist();//调用用户出拳方法compFist = computer.showFist();//调用计算机出拳方法//裁决if((perFist == 1 && compFist == 1) ||(perFist == 2 && compFist == 2) ||(perFist == 3 && compFist == 3)){System.out.println("结果:和局,真衰!\n"); //平局}else if((perFist == 1 && compFist == 3) ||(perFist == 2 && compFist == 1) ||(perFist == 3 && compFist == 2)){System.out.println("结果:恭喜, 你赢了!\n"); //用户赢person.score++; //累计用户积分}else{System.out.println("结果说:^_^,你输了,真笨!\n");//计算机赢computer.score++; //累计计算机积分}count++;//累计对战次数System.out.print("是否开始下一轮(y/n):");answer = input.next();}}} //比较得分情况的方法public void showResult(){System.out.println("------------------------------------------------------");System.out.println( + " VS " + );System.out.println("对战次数:" + count);System.out.println("\n姓名\t得分\n" + + "\t" + person.score+ "\n" + + "\t" + computer.score + "\n");//比较积分if(computer.score == person.score){System.out.println("结果:打成平手,下次再和你一分高下!");}else if(computer.score < person.score){System.out.println("结果:你果然是高手," + + "佩服!");}else{System.out.println("结果:呵呵,笨笨,下次加油哦!");}System.out.println("------------------------------------------------------");}}public class Computer {String name;//名字属性int score;//积分属性//出拳方法public int showFist(){int num = (int)(Math.random()*3) + 1;String fist = "";switch(num){case 1:fist = "剪刀";break;case 2:fist = "石头";break;case 3:fist = "布";break;}System.out.println(name + "出拳:" + fist);return num;}}运行界面:3、实验心得体会:从本次课程设计的完成中,我发现我有很多不足的地方,最突出的是所掌握的知识太少,学到的知识应用不到实践中。
Java猜拳⼩游戏(剪⼑、⽯头、布)1、第⼀种实现⽅法,调⽤Random数据包,直接根据“1、2、3”输出“剪⼑、⽯头、布”。
主要⽤了9条输出判断语句。
import java.util.Random;import java.util.Scanner;public class caiquan{public static void main(String[] args){Random r=new Random();int diannao=r.nextInt(3)+1;Scanner s=new Scanner(System.in);System.out.println("=========猜拳⼩游戏=========");System.out.println("请输⼊1、2、3,1代表剪⼑,2代表⽯头,3代表布");int fangke=s.nextInt();if(diannao==1&&fangke==1){System.out.println("电脑出的是剪⼑,你出的是剪⼑,平局");}if(diannao==1&&fangke==2){System.out.println("电脑出的是剪⼑,你出的是⽯头,你赢了");}if(diannao==1&&fangke==3){System.out.println("电脑出的是剪⼑,你出的是布,电脑赢了");}if(diannao==2&&fangke==1){System.out.println("电脑出的是⽯头,你出的是剪⼑,电脑赢了");}if(diannao==2&&fangke==2){System.out.println("电脑出的是⽯头,你出的是⽯头,平局");}if(diannao==2&&fangke==3){System.out.println("电脑出的是⽯头,你出的是布,你赢了");}if(diannao==3&&fangke==1){System.out.println("电脑出的是布,你出的是剪⼑,你赢了");}if(diannao==3&&fangke==2){System.out.println("电脑出的是布,你出的是⽯头,电脑赢了");}if(diannao==3&&fangke==3){System.out.println("电脑出的是布,你出的是布,平局");}if(fangke!=1&&fangke!=2&&fangke!=3){System.out.println("只能输⼊1、2、3");}}} 2、第⼆种实现⽅法,不调⽤Random数据包,换成Math.random(),把“1、2、3”换成“剪⼑、⽯头、布”再输出。
java实现简单⽯头剪⼑布游戏本⽂实例为⼤家分享了java实现简单⽯头剪⼑布游戏的具体代码,供⼤家参考,具体内容如下问题描述Alice, Bob和Cindy⼀起玩猜拳的游戏。
和两个⼈的猜拳类似,每⼀轮,他们会从⽯头、剪⼑、布中各⾃选⼀个出拳,基本的胜负规则是⽯头赢剪⼑、剪⼑赢布、布赢⽯头。
如果⼀轮中正好可以分成胜负两边,则负边的每个⼈要⽀付给胜边的每个⼈⼀块钱。
如果⽆法分成胜负两边,则都不出钱。
⽐如,如果Alice出⽯头,⽽Bob和Cindy都出布,则Alice要分⽀付Bob和Cindy⼀块钱。
再如,如果Alice出⽯头, Bob出剪⼑,Cindy出布,则都不出钱。
他们三⼈共进⾏了n轮游戏,请问最后每个⼈净赚多少钱?即赚的钱减去⽀付的钱是多少?代码package Ring1270.pra.java01;import java.util.Scanner;/*** finger-guessing game: * n:number of games * A: Person A's money * B: Person B's money * C: Person C's money * 0: Stand for stone * 1: Stand for Scissor * 2: Stand for cloth * rule1: Two persons give the same result means game over * Rule2: The money public static void main(String[] args) {int A = 0;int B = 0;int C = 0;Scanner scanner = new Scanner(System.in);System.out.printf("The number of game:");int n = scanner.nextInt();StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i <= n; i++) {String s = scanner.nextLine();char[] D = s.toCharArray();for (int j = 0; j < D.length; j++) {//A and B successif (D[0] == D[1] && D[0] != D[2]) {if ('0' == D[0] && '1' == D[2]) {A++;B++;C -= 2;}else if ('1' == D[0] && '2' == D[2]) {A++;B++;C -= 2;}else if ('2' == D[0] && '0' == D[2]) {A++;B++;C -= 2;}else {A--;B--;C += 2;}}// A and C successif (D[0] == D[2] && D[0] != D[1]) {if ('0' == D[0] && '1' == D[1]) {A++;B -= 2;C++;}else if ('1' == D[0] && '2' == D[1]) {A++;B -= 2;C++;}else if ('2' == D[0] && '0' == D[1]) {A++;B -= 2;C++;}else {A--;B += 2;C--;}}// C and B successif (D[1] == D[2] && D[1] != D[0]) {if ('0' == D[1] && '1' == D[0]) {A -= 2;B++;C++;}else if ('1' == D[1] && '2' == D[0]) {A -= 2;B++;C++;}else if ('2' == D[1] && '0' == D[0]) {A -= 2;B++;C++;}else {A += 2;B--;C--;}}break;}}System.out.println(A);System.out.println(B);System.out.println(C);}}运⾏截图以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
Java猜拳小游戏程序设计实验报告第一篇:Java猜拳小游戏程序设计实验报告Java程序设计实验报告班级:学号:姓名:实验题目:猜拳小游戏实验要求:用java编写一个人机对战的猜拳小游戏。
人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况。
有简单的操作界面。
实验内容:1、问题分析过程:(1)首先分析猜拳游戏本身的玩法:人选择性出拳,电脑随机出拳,判断输赢,记录输赢情况。
(2)用面向对象的思想来分析:在游戏过程中有几个对象组成人电脑游戏规则抽象出3个类:Person、Computer、Game Person类有哪些属性和行为呢?属性:名字name,输赢次数(比分)score 行为:出拳ShowFirst()选择性Computer类有哪些属性和行为呢?属性:名字name,输赢次数(比分)score 行为:出拳showFist()随机Game类有哪些属性和行为呢?属性:游戏的双方(person、computer)、对战局数count 行为:产生角色initial()、游戏规则startGame()、显示比赛结果showResult()、统计比赛结果calcResul()2、主要实现代码:import java.util.*;public class StartGame { public static void main(String[]args){Game start = new Game();//实例化游戏类start.initial();//调用初始化方法start.startGame();//调用游戏开始方法start.showResult();//调用游戏结果显示方法} } import java.util.*;public class Person { String name;//名字属性int score;//积分属性//出拳方法public int showFist(){System.out.print(“n请出拳:1.剪刀2.石头3.布(输入相应数字):”);Scanner input = new Scanner(System.in);int num = input.nextInt();String fist = “";//保存出拳switch(num){ case 1:fist = ”剪刀“;break;case 2:fist = ”石头“;break;case 3:fist = ”布“;break;}System.out.println(name + ”出拳:“ + fist);return num;} } import java.util.*;public class Game { //Person person;//甲方//Computer computer;//乙方int count;//对战次数Person person = new Person();//实例化用户类Computer computer = new Computer();//实例化计算机类//初始化方法public int initial(){count = 0;return count;} //游戏开始方法public void startGame(){//显示游戏开始界面System.out.println(”---------------欢迎进入游戏世System.out.println(“tt******************************”);System.out.println(“tt**^_^ 猜拳,Start ^_^**”);System.out.println(“tt*****************************”);界--------------n“);System.out.println(”nn出拳规则:1.剪刀 2.石头 3.布“);//选择计算机角色System.out.print(”请选择对方角色:1.刘备2.孙权3.曹操:“);Scanner input = new Scanner(System.in);int num = input.nextInt();switch(num){ case 1: = ”刘备“;break;case 2: = ”孙权“;break;case 3: } = ”曹操“;break;//输入用户角色名System.out.print(”请输入你的姓名:“); = input.next();//显示对战双方System.out.print( + ” VS “ + + ” 对战n“);//开始游戏System.out.print(”n要开始吗?(y/n)“);String con = input.next();int perFist;//用户出的拳 int compFist;//计算机出的拳if(con.equals(”y“)){//判断是否开始String answer = ”y“;while(”y“.equals(answer)){//循环条件是是否开始下一轮//出拳perFist = person.showFist();//调用用户出拳方法compFist = computer.showFist();//调用计算机出拳方法//裁决if((perFist == 1 && compFist == 1)||(perFist == 2 && compFist == 2)||(perFist == 3 && compFist == 3)){Syst em.out.println(”结果:和局,真衰!n“);//平局}else if((perFist == 1 && compFist == 3)||(perFist == 2 && compFist == 1)||(perFist == 3 && compFist == 2)){System.out.println(”结果:恭喜, 你赢了!n“);//用户赢person.score++;//累计用户积分}else{}} System.out.println(”结果说:^_^,你输了,真笨!n“);//计算机赢computer.score++;//累计计算机积分 } count++;//累计对战次数System.out.print(”是否开始下一轮(y/n):“);answer = input.next();}//比较得分情况的方法public void showResult(){ System.out.println(”-----------------------“);System.out.println( + ” VS “ + person.na me);System.out.println(”对战次数:“ + count);System.out.println(”n姓名t得分n“ + + ”t“ + person.score+ ”n“ + + ”t“ + computer.score + ”n“);//比较积分if(computer.score == person.score){System.out.println(”结果:打成平手,下次再和你一分高下!“);}else if(computer.score < person.score){System.out.println(”结果:你果然是高手,“ + + ”佩服!“);}else{System.out.println(”结果:呵呵,笨笨,下次加油哦!“);}System.out.println(”-----------------------“);} } public class Computer {String name;//名字属性 int score;//积分属性 //出拳方法public int showFist(){ int num =(int)(Math.random()*3)+ 1;String fist = ”“;switch(num){ case 1:fist = ”剪刀“;break;case 2:fist = ”石头“;break;case 3:fist = ”布“;break;}System.out.println(name + ”出拳:" + fist);return num;} } 运行界面:3、实验心得体会:从本次课程设计的完成中,我发现我有很多不足的地方,最突出的是所掌握的知识太少,学到的知识应用不到实践中。
Project:guessgame Package:.hpu.guess【Computer.java】package .hpu.guess;import java.util.Random;/*** 电脑玩家类* @author Administrator**/publicclass Computer {//电脑玩家的姓名private String name;//电脑玩家的积分privateint score;//电脑玩家出拳的方法publicint showFist(){Randomrandom = new Random();int fist = random.nextInt(3) + 1;if(fist == 1){System.out.println(name+"出拳:剪刀");}elseif(fist == 2){System.out.println(name+"出拳:石头");}elseif(fist == 3){System.out.println(name+"出拳:布");}return fist;}}publicvoid setName(String name) { = name;}publicint getScore() {return score;}publicvoid setScore(int score) { this.score = score;}}【Game.java】package .hpu.guess;import java.util.Scanner;publicclass Game {//玩家private Player player = new Player();//电脑玩家private Computer computer = new Computer();//总局数privateint count = 0;//开始游戏publicvoid start(){Scanner input = new Scanner(System.in);//输出游戏规则System.out.println("---------欢迎进入猜拳游戏-----------");System.out.println("猜拳规则:1.剪刀 2.石头 3.布");//选择对战角色System.out.print("请选择对手:1.刘备 2.曹操 3.孙权(请输入数字):");int selectRole = input.nextInt();System.out.println("selectRoe="+selectRole) ;if(selectRole == 1){computer.setName("刘备");}elseif(selectRole == 2){computer.setName("曹操");}elseif(selectRole == 3){computer.setName("孙权");}//输入尊姓大名System.out.print("请输入您的尊姓大名:");String name = input.next();player.setName(name);System.out.print("开始游戏吗?yes/no:");StringisBegin = input.next();if(isBegin.equals("yes")){while(true){//玩家出拳int playerFist = player.showFist();//电脑玩家出拳int computerFist =computer.showFist();//根据出拳结果判断本局输赢if((playerFist == 1 &&computerFist == 3) || (playerFist == 2 &&computerFist == 1) || (playerFist == 3 &&computerFist == 2)){System.out.println("本局"+player.getName()+"赢.");player.setScore(player.getScore() + 1);}elseif((playerFist == 1&&computerFist == 1) || (playerFist == 2&&computerFist == 2) || (playerFist == 3&&computerFist == 3)){System.out.println("本局平.");}else{System.out.println("本局"+computer.getName()+"赢.");computer.setScore(computer.getScore() + 1);}count++;System.out.println("游戏继续吗?yes/no:");StringisContinue = input.next();if(!isContinue.equals("yes")){break;}}}}/*** 统计游戏结果*/publicvoid showResult(){System.out.println("-----------------比赛结果------------------");System.out.println("对战双方:"+player.getName() +" vs "+ computer.getName());System.out.println("一共比赛了"+count+"局");System.out.println(player.getName()+"赢了"+player.getScore()+"局.");System.out.println(computer.getName()+"赢了"+computer.getScore()+"局.");System.out.println("平"+(count-player.getScore()-computer.getScore() )+"局");if(player.getScore() >computer.getScore()) {System.out.println("最终结果:"+player.getName()+"赢.");}elseif(player.getScore()<computer.getScore()){System.out.println("最终结果:"+computer.getName()+"赢.");}else{System.out.println("最终结果:平局.");}}}【MainTest.java】package .hpu.guess;publicclass MainTest {publicstaticvoid main(String[] args) { Game game = new Game();game.start();game.showResult();/*Random random = new Random();for(inti=1; i<=10; i++){int number = random.nextInt(10);System.out.println(number);}*/}}【Player.java】package .hpu.guess;import java.util.Scanner; /*** 玩家类* @author Administrator **/publicclass Player {//玩家的姓名private String name;//玩家的积分privateint score;//出拳的方法publicint showFist(){Scanner input = new Scanner(System.in);System.out.print("请出拳:1.剪刀 2.石头 3.布(请输入数字)");int fist = input.nextInt();if(fist == 1){System.out.println(name+"出拳:剪刀");}elseif(fist == 2){System.out.println(name+"出拳:石头");}elseif(fist == 3){System.out.println(name+"出拳:布");}return fist;}public StringgetName() {return name;}publicvoid setName(String name) { = name;}publicint getScore() {return score;}publicvoid setScore(int score) { this.score = score;}}。
java猜拳游戏出拳规则:1.剪刀2.石头3.布请选择对方角色(1:刘备2.孙权3.曹操)package sjlx.guess;import java.util.*;public class Computer extends Player {public Computer(){}public Computer(String name, int score){ super(name,score); }public void setScore(int score){super.setScore(score);}public String showFist(int choice){String cFist = " ";switch(choice){case 1:cFist = "剪刀";break;case 2:cFist = "石头";break;default:cFist = "布";break;}return cFist;}}====================================== ========================================= package sjlx.guess;import java.util.Scanner;public class Game {Computer cpt;User usr;int count;Scanner input = new Scanner(System.in);//初始化游戏public void initial(){System.out.println("*****猜拳开始*****");System.out.println("出拳规则:1.剪刀 2.石头 3.布");//选择对方的角色System.out.print("请选择对方角色(1:刘备 2:孙权 3:曹操):");int choice = input.nextInt();String name=" ";switch(choice){case 1:name = "刘备";break;case 2:name= "孙权";break;case 3:name = "曹操";break;}cpt = new Computer(name,0);//确定用户姓名System.out.print("输入你的姓名:");name = input.next();usr = new User(name,0);//显示对战信息System.out.println(cpt.getName()+" VS "+usr.getName()+" 对战");System.out.println("----------------------------");}//开始游戏public void start(){System.out.print("要开始吗?(y/n)");String answer= input.next(); //是否开始游戏while(answer.equals("y")){System.out.println();System.out.print("请出拳:1.剪刀2.石头3.布(请输入相应数字)");String uFist = usr.showFist(input.nextInt()); //用户出拳System.out.println();System.out.println("你出拳:"+uFist);//生成1~3之间的随机数int choice = (int)(Math.random()*3)+1;String cFist = cpt.showFist(choice); //计算机随机出拳System.out.println(cpt.getName()+"出拳:"+cFist);gameResult(uFist,cFist); //计算对战结果count++; // 对战次数加1System.out.println();System.out.print("是否开始下一轮(y/n):");answer = input.next();}showGamesResult();}//计算对战结果public void gameResult(String userFist, String cmpFist){if((userFist == "剪刀" && cmpFist == "布") ||(userFist == "石头" && cmpFist == "剪刀")||(userFist == "布" && cmpFist == "石头")){System.out.println("结果说:你赢了!");usr.setScore(usr.getScore()+1); //积分+1}else if(userFist.equals(cmpFist)){System.out.println("结果说:平局!!");}else{System.out.println("结果说:你输了!");cpt.setScore(cpt.getScore()+1);}}//显示对战结果public void showGamesResult(){System.out.println("----------------------------");System.out.println(cpt.getName()+" vs "+usr.getName());System.out.println("对战次数"+count);System.out.println("姓名\t得分");System.out.println(usr.getName()+"\t"+usr.getScore());System.out.println(cpt.getName()+"\t"+cpt.getScore());if(usr.getScore() > cpt.getScore()){System.out.println("结果:你赢了!");}else{System.out.println("结果:下次加油啊!");}System.out.println("----------------------------");}}====================================== ========================================= package sjlx.guess;public abstract class Player {private String name; //玩家姓名private int score; //玩家积分public Player(){}public Player(String name, int score){ = name;this.score = score;}public String getName(){return name;}public int getScore(){return score;}public void setScore(int score){this.score = score;}public abstract String showFist(int choice);}====================================== ========================================= package sjlx.guess;import java.util.Scanner;public class Test {public static void main(String[] args){Scanner input = new Scanner(System.in);String answer="";do{Game gm = new Game();gm.initial();gm.start();System.out.print("开始下一局吗?(y/n):");answer = input.next();}while(answer.equals("y"));System.out.println("系统退出!");}}====================================== ========================================= package sjlx.guess;public class User extends Player {public User(){}public User(String name, int score){super(name,score);}public void setScore(int score){ super.setScore(score);}public String showFist(int choice){ String cFist;switch(choice){case 1:cFist = "剪刀";break;case 2:cFist = "石头";break;default:cFist = "布";break;}return cFist;}}。
java猜拳游戏实验报告Java 猜拳游戏实验报告一、实验目的本实验旨在通过使用 Java 编程语言实现一个猜拳游戏,以加深对Java 基础语法、面向对象编程思想以及程序逻辑控制的理解和运用。
二、实验环境1、操作系统:Windows 102、开发工具:IntelliJ IDEA3、 JDK 版本:18三、实验原理猜拳游戏的核心逻辑是模拟玩家和计算机的出拳选择,并根据出拳结果判断胜负。
在 Java 中,可以使用随机数生成计算机的出拳,通过用户输入获取玩家的出拳,然后比较两者的出拳来确定胜负关系。
四、实验步骤1、定义游戏相关的常量和变量定义表示石头、剪刀、布的常量,如`STONE`、`SCISSORS`、`PAPER`。
定义表示玩家和计算机得分的变量。
2、实现获取用户输入的方法使用`Scanner` 类获取用户输入的数字,代表出拳选择(1 表示石头,2 表示剪刀,3 表示布)。
3、实现计算机出拳的方法使用`Random` 类生成一个 1 到 3 之间的随机数,代表计算机的出拳。
4、实现判断胜负的方法根据玩家和计算机的出拳,比较两者的大小,确定胜负关系,并更新得分。
5、实现游戏主逻辑在主方法中,循环进行游戏,直到玩家选择退出。
每次循环中,获取玩家和计算机的出拳,判断胜负,输出结果,并询问玩家是否继续游戏。
五、核心代码实现```javaimport javautilRandom;import javautilScanner;public class RockPaperScissorsGame {//定义表示石头、剪刀、布的常量private static final int STONE = 1;private static final int SCISSORS = 2;private static final int PAPER = 3;public static void main(String args) {int playerScore = 0;int computerScore = 0;Scanner scanner = new Scanner(Systemin);Random random = new Random();while (true) {//获取玩家出拳Systemoutprintln("请选择出拳(1、石头 2、剪刀 3、布):");int playerChoice = scannernextInt();//生成计算机出拳int computerChoice = randomnextInt(3) + 1;//判断胜负int result = judgeWinner(playerChoice, computerChoice);if (result == 1) {playerScore++;Systemoutprintln("你赢了!");} else if (result ==-1) {computerScore++;Systemoutprintln("你输了!");} else {Systemoutprintln("平局!");}Systemoutprintln("你的得分:"+ playerScore +"计算机得分:"+ computerScore);Systemoutprintln("是否继续游戏?(Y/N)");String continueChoice = scannernext();if (!continueChoiceequalsIgnoreCase("Y")){break;}}}public static int judgeWinner(int playerChoice, int computerChoice) {if (playerChoice == computerChoice) {return 0;} else if ((playerChoice == STONE && computerChoice ==SCISSORS) ||(playerChoice == SCISSORS && computerChoice ==PAPER) ||(playerChoice == PAPER && computerChoice == STONE)){return 1;} else {return -1;}}}```六、实验结果与分析1、运行程序后,能够成功获取玩家的输入,并生成计算机的随机出拳。
java人机猜拳
1.首先定义一个用户类:代码如下
package mypackage;
import java.util.*;
public class Person {
String name="";
int score;
public int showFist()
{
System.out.println("请出拳:1.剪刀2.石头3.布(输入相应数字)");
Scanner input =new Scanner(System.in);
int number=input.nextInt();
switch(number)
{
case 1:
System.out.println("玩家出:剪刀");
return number;
case 2:
System.out.println("玩家出:石头");
return number;
case 3:
System.out.println("玩家出:布");
return number;
default:
System.out.println("你出拳:剪刀");
return number;
}
}
}
2.定义一个计算机类
package mypackage;
public class Computer {
int max =3;
int min =1;
int number= (int)(Math.random()*(max-min))+min;
int score;
String name="电脑";
public int showcomputer()
{
switch(number)
{
case 1:
System.out.println("电脑出:剪刀");
return number;
case 2:
System.out.println("电脑出;石头");
return number;
case 3:
System.out.println("电脑出:布");
return number;
default:
System.out.println("电脑出:剪刀");
return number;
}
}
}
3.创建一个游戏类
package mypackage;
import java.util.*;
public class StartGame
{
public int Initial()
{
System.out.println("----------欢迎进入游戏世界----------");
System.out.println("");
System.out.println("\t****************");
System.out.println("\t** 猜拳,开始**\t\t");
System.out.println("\t****************");
System.out.println("");
System.out.println("出拳规则:1.剪刀2.石头3.布");
System.out.println("请选择对方角色(1.刘备2.孙权3.曹操):");
Scanner input =new Scanner(System.in);
int number=input.nextInt();
switch(number)
{
case 1:
System.out.print("刘备");
return number;
case 2:
System.out.print("孙权");
return number;
case 3:
System.out.print("曹操");
return number;
default:
System.out.print("你选择了刘备作战");
return number;
}
}
public static void main(String[] args)
{
//完善游戏类的startGame()方法,实现一局对战
Computer computer =new Computer();
Person player =new Person();
StartGame come =new StartGame();
Scanner input =new Scanner(System.in);
come.Initial();
System.out.println("");
System.out.println("要开始么?y/n\n\n");
String con =input.next();
int count=0;
while(con.equalsIgnoreCase("y"))
{
int perFist=player.showFist();
int compFist=computer.showcomputer();
System.out.println("双方对战次数:" + count);
if((perFist==1&&compFist==1)||(perFist==2&&compFist==2)||(perFist==3&&com pFist==3))
{
System.out.println("结果:平局,真衰!");
count++;
}else
if((perFist==1&&compFist==3)||(perFist==2&&compFist==1)||(perFist==3&&compFist ==2))
{
System.out.println("结果:恭喜,你赢了!");
player.score++;
}
else{
System.out.println("结果说,你输了,真笨!\n");
count++;
computer.score++;
}
System.out.println(+ "积分为:" + player.score+ "\t\t" + + "积分为:" + computer.score);
System.out.println("是否继续?y/n");
con =input.next();
}
while(con.equals("n"))
{
if(player.score > computer.score) {
System.out.println("最终结果:" + + "在" + count + "回合中战胜了" + );
break;
}
else if(player.score < computer.score) {
System.out.println("最终结果:" + + "在" + count + "回合中战胜了" + );
break;
}
else {
System.out.println("最终结果:" + + "在" + count + "回合中和" + + "战平");
break;}
}
}
}
本代码归武汉市江岸区百步亭50号熊盼所有,未经武汉市江岸区百步亭50号熊盼允许,
不得转载、复制。
联系QQ:457554179
地址;武汉市江岸区百步亭50号。