人机猜拳

import java.util.Scanner;
 public class Person { Scanner input = new Scanner(System.in);  String name; int score; String action; int num;  public void method() { System.out.println("\n请出拳:1.剪刀 2.石头 3.布"); boolean a = true; do { num = input.nextInt(); if (num == 1 || num == 2 || num == 3) { switch (num) { case 1: action = "剪刀"; break; case 2: action = "石头"; break; case 3: action = "布"; break; } a = false; System.out.println("你出拳:" + action); } else { System.out.println("输入数字有误,请重新输入"); } } while (a); } }

第二个类,电脑类:

  public class Computer {  String name; int score; String action; int num;  public void method(){  num=(int)((Math.random())*3)+1; switch(num){ case 1: action="剪刀"; break; case 2: action="石头"; break; case 3: action="布"; break; } System.out.println(name+"出拳:"+action); } } 

第三个类,游戏类:

import java.util.Scanner;

public class Game { Scanner input = new Scanner(System.in);  Person person = new Person();//创建玩家类的对象 Computer computer = new Computer();//创建电脑类的对象 int number; int frequency = 0;  public void process() { System.out .println("--------------------------欢迎进入游戏世界--------------------------\n"); System.out.println("\t\t********************************"); System.out.println("\t\t**********猜拳,开始*************"); System.out.println("\t\t********************************"); System.out.println(); System.out.println("出拳规则:1.剪刀 2.石头 3.布"); System.out.print("请选择对方角色(1:刘备2:孙权3:曹操):"); boolean b = true; do { number = input.nextInt(); if (number == 1 || number == 2 || number == 3) { switch (number) { case 1: computer.name = "刘备"; break; case 2: computer.name = "孙权"; break; case 3: computer.name = "曹操"; break; } b = false; } else { System.out.println("输入数字有误,请重新输入"); } } while (b); System.out.print("请输入你的姓名:"); person.name = input.next(); System.out.println(person.name + " VS " + computer.name + " 对战\n"); System.out.println("要开始吗?(y/n)"); char answer = input.next().charAt(0); while (answer == 'y') { person.method(); computer.method(); if (person.num == computer.num) { System.out.println("嘿嘿,和局,等着瞧吧!"); } else if (((person.num == 2) && (computer.num == 1)) || (person.num == 1) && (computer.num == 3) || ((person.num == 3) && (computer.num == 2))) { System.out.println("哇,你赢了,好厉害!"); person.score++; } else { System.out.println("^_^!!!你输了,真笨!"); computer.score++; } frequency++; System.out.println("\n"); System.out.println("还要继续吗?(y/n)"); answer = input.next().charAt(0); } }  public void showResult() { System.out.println("********************************"); System.out.println(person.name + " VS " + computer.name); System.out.println("对战次数:" + frequency); System.out.println("\n姓名\t\t得分"); System.out.println(person.name + "\t\t" + person.score); System.out.println(computer.name + "\t\t" + computer.score); if (person.score < computer.score) { System.out.println("呵呵,笨笨,下次加油!"); } else if (person.score == computer.score) { System.out.println("哇,竟然平局,我们下次一决胜负!"); } else { System.out.println("哇,你好棒啊!"); } System.out.println("********************************"); } }

测试类:

public class Demo {
public static void main(String[] args) { Game play=new Game();//创建游戏类的对象 play.process();//调用游戏类的游戏进程方法 play.showResult();//调用游戏类的结算方法 } }

转载于:https://www.cnblogs.com/Chencheno/p/9789141.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值