类的无参方法(人机猜拳)

来源:互联网 发布:笔记本电脑软件打开慢 编辑:程序博客网 时间:2024/05/03 19:24
用户的类:<pre name="code" class="java">public class Computer {// 定义姓名和分数的对象String name;int score;public int show() {// 建立随机数int min = 1;int max = 3;int shu = (int) (Math.random() * max) + min;System.out.print("电脑出拳:");switch (shu) {case 1:System.out.println("剪刀");break;case 2:System.out.println("石头");break;case 3:System.out.println("布");break;}return shu;// 返回shu的值}}

电脑的类:
<pre name="code" class="java">public class Computer {// 定义姓名和分数的对象String name;int score;public int show() {// 建立随机数int min = 1;int max = 3;int shu = (int) (Math.random() * max) + min;System.out.print("电脑出拳:");switch (shu) {case 1:System.out.println("剪刀");break;case 2:System.out.println("石头");break;case 3:System.out.println("布");break;}return shu;// 返回shu的值}}
游戏运行的类:
import java.util.*;public class Game {test ren;Computer computer;//创立对象String juese;//创立对象int name;int count;public void initial() {//对象的实例化computer = new Computer();ren = new test();}public void Gameshow() {initial();//调用initial方法boolean con;Scanner input = new Scanner(System.in);System.out.print("请输入你的姓名:");juese = input.next();do {con = true;System.out.println("************欢迎进入游戏世界**************\n\n");System.out.println("\t\t************************");System.out.println("\t\t***  猜拳开始  ***");System.out.println("\t\t************************\n\n\n");System.out.println("出拳规则:1.剪刀   2.石头   3.布");System.out.println("请选择对方角色:(1.刘备  2.曹操  3.孙权)");name = input.nextInt();switch (name) {case 1:computer.name = "刘备";break;case 2:computer.name = "曹操";break;case 3:computer.name = "孙权";break;}System.out.print(computer.name);System.out.println();Scanner input1 = new Scanner(System.in);System.out.println("要开始吗?(y/n)");String can = input1.next();int renfist;int comfist;if ("y".equals(can)) {/** * @return人的返回值 */renfist = ren.showren();/** * @return电脑的返回值 */comfist = computer.show();//将人的返回值和电脑的返回值进行比较来判断谁输谁赢if ((renfist == 1 && comfist == 1)|| (renfist == 2 && comfist == 2)|| (renfist == 3 && comfist == 3)) {System.out.println("结果:和局!\n");} else if ((renfist == 1 && comfist == 3)|| (renfist == 2 && comfist == 1)|| (renfist == 3 && comfist == 2)) {System.out.println("结果:你赢了!\n");ren.score++;//计算人赢得次数} else {System.out.println("结果:你输了!\n");computer.score++;//计算电脑赢得次数}count++;//计算总次数} else {con = false;//跳出循环}} while (con);System.out.println("----------------------------");System.out.println(computer.name + "VS" + juese);System.out.println("对战次数" + count);System.out.println(juese + "赢的次数: " + ren.score +computer.name +"赢的次数:" + computer.score);//将人的分数和电脑的分数进行比较if (computer.score == ren.score) {System.out.println("平局");} else if (computer.score < ren.score) {System.out.println(juese + "赢了" + computer.name + "输了");} else {System.out.println(computer.name + "赢了" + juese + "输了");}}}
调用游戏的类:
public class kai {public static void main(String[] args) {Game de = new Game();de.Gameshow();}}



                                             
0 0
原创粉丝点击