




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
先建立个Game包然后我做的是分了5个类来做的TestStartGuess 类package com.game.guess;public class TestStartGuess /* * 人机互动版猜拳游戏 * 程序入口 */public static void main(String args) Game game=new Game();game.initial();game.startGame();2.Person 类package com.game.guess;import java.util.Scanner;/* * 用户类 *阶段1完成 * param */public class Person String name =匿名;/名字int score =0;/积分/* 出拳*return出拳结果:1.剪刀 2.石头 3.布*/public int showFist()/接收用户的选择Scanner input =new Scanner(System.in);System.out.print(n请出拳:1.剪刀 2.石头 3.布 (输入相应数字):);int show=input.nextInt();/输出出拳结果,并返回switch(show)case 1:System.out.println(你出拳:剪刀);break;case 2:System.out.println(你出拳:石头);break;case 3:System.out.println(你出拳:布);break;return show;3.Computer 类package com.game.guess;/*计算机类*阶段2完成*/public class Computer String name=电脑;/名字 int score = 0;/积分 /* *出拳 *return 出拳结果:1.剪刀 2.石头 3.布 */ public int showFist() /产生随机数 int show =(int)(Math.random()*10)%3+1;/产生随机数,表示电脑出拳 /输出出拳结果并返回 switch(show)case 1:System.out.println(name+你出拳:剪刀);break;case 2:System.out.println(name+你出拳:石头);break;case 3:System.out.println(name+你出拳:布);break;return show;4.Game 类package com.game.guess;import java.util.Scanner;/* * 游戏类类完全版 * 阶段7:功能扩展 * param * */public class Game Person person; /甲方Computer computer; /乙方int count;/对战次数/* 初始化*/public void initial()person=new Person();computer=new Computer();count=0;/* 开始游戏*/SuppressWarnings(resource)public void startGame()System.out.println(-欢迎进入游戏世界-n);System.out.println(ntt*);System.out.println(tt*猜拳,开始 *);System.out.println(tt*);System.out.println(nn出拳规则:1.剪刀,2.石头,3.布);Scanner input=new Scanner(System.in);String exit=n; /退出系统doinitial();/初始化/*选择对方角色*/System.out.print(请选择对方角色:(1:刘备,2:孙权,3:曹操):);int role=input.nextInt();if(role=1)=刘备;else if(role=2)=孙权;else if(role=3)=曹操;/扩展功能1:输入用户姓名/*输入用户姓名*/System.out.print(请输入你的姓名:);=input.next();System.out.println(+VS++对战n);/扩展功能1结束System.out.print(要开始吗?(y/n);String start=input.next();/开始每一局游戏int perFist; /用户出的拳int compFist; /计算机出的拳while(start.equals(y)/*出拳*/ 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(结果:恭喜,你赢了!); /用户赢 person.score+; else System.out.println(结果说:_,你输了,真笨!n); /计算机赢 computer.score+; count+; System.out.println(n是否开始下一轮(y/n):); start=input.next(); /*显示结果*/showResult();/扩展功能3:循环游戏,知道退出系统System.out.print(n要开始下一局吗?(y/n):);exit=input.next(); System.out.println(); /扩展功能3结束while(!exit.equals(n);System.out.println(系统退出!);/* 显示比赛结果*/public void showResult()/*显示对战次数*/System.out.println(-); System.out.println(+VS+); System.out.println(对战次数:+count); /扩展功能2:显示最终的得分 System.out.println(n姓名t得分); System.out.println(+t+person.score);System.out.println(+t+computer.score+n); /扩展功能2结束 /*显示对战结果*/ int result=calcResult(); if(result=1) System.out.println(结果:打成平手,下次再和你一分高下!); else if(result=2) System.out.println(结果:恭喜恭喜!); /用户获胜 else System.out.println(结果:呵呵,笨笨,下次加油啊!); /计算机获胜 System.out.println(-); /* 计算比赛结果* return1:战平; 2:用户赢; 3:电脑赢*/public int calcResult()if(person.score=computer.score)return 1;/战平else if(person.scorecomputer.score)return 2;/用户赢
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 建材采购流程及成本控制方案
- 劳动合同法律条款解析指南
- 大专隧道施工技术
- 肝胆内科出科汇报
- 初级检验员岗位技能培训教材
- 贸易协定投资条款解读
- 小学科学探究实验课设计方案
- 2023年初中英语感叹句专项训练方案
- 合同管理办法教学课件
- 合同法律文书撰写实务指南
- 重症自身免疫性脑炎监测与治疗中国专家共识(2024版)解读
- 机动车检测工资格证考试题(附答案)
- 护士沟通技巧与人文关怀护理课件
- 2025年上半年海南三亚市知识产权保护中心选聘事业单位6人重点基础提升(共500题)附带答案详解
- 2025年辽宁现代服务职业技术学院单招综合素质考试题库附答案
- 电力电缆模拟题及答案
- 2025年药物制剂工(中级)考试题库(附答案)
- 仿古建筑施工常见问题及应对策略
- 辽宁省沈阳市2024-2025学年八年级上学期期末考试英语试题(含答案无听力原文及音频)
- 小班晨间活动体能大循环
- 绿化小型工程合同范例
评论
0/150
提交评论