人机猜拳

//玩家类
import
java.util.Scanner; public class Person{ String name; int number; public int showFist(){ Scanner input=new Scanner(System.in); System.out.println("请出拳:1.剪刀2.石头3.布"); number =input.nextInt(); switch(number){ case 1: System.out.println("你出拳:剪刀"); break; case 2: System.out.println("你出拳:石头"); break; case 3: System.out.println("你出拳:布"); break; } return number; }
//电脑类
public
class Computer { String name; int number; public int showFist() {
//产生随机数
int sjs=(int)(Math.random()*3+1); switch(sjs) { case 1: System.out.println("对手出拳:剪刀"); break; case 2: System.out.println("对手出拳:石头"); break; case 3: System.out.println("对手出拳:布"); break; } return sjs; } }
//游戏类和测试类
import
java.util.Scanner; public class Game { public static void main(String[] args) { String name; String person; int number; String computer; int count=0; int perFist; int compFist; System.out.println(" -----------------欢迎进入游戏世界----------------- "); System.out.println(" **********************"); System.out.println(" **猜拳,开始**"); System.out.println(" **********************");
//创建对象 Person person1
=new Person(); Computer computer1=new Computer(); System.out.println("出拳规则:1.剪刀2.石头3.布"); Scanner input = new Scanner(System.in); System.out.println("请选择对方角色(1:刘备2:孙权3:曹操)"); number = input.nextInt(); System.out.println("请输入你的姓名:"); name=input.next(); switch (number) { case 1: System.out.println("你选择了刘备对战"); break; case 2: System.out.println("你选择了孙权对战"); break; case 3: System.out.println("你选择了曹操对战"); break; } System.out.println(" 要开始吗?(y/n)"); String con = input.next(); boolean duo=true; char num; do { if (con.equals("y")) { perFist=person1.showFist(); compFist=computer1.showFist(); if ((perFist == 1 && compFist == 1) || (perFist == 2 && compFist == 2) || (perFist == 13 && compFist == 3)) { System.out.println("结果:和局,真衰! "); //平局 } else if ((perFist == 1 && compFist == 3) || (perFist == 2 && compFist == 1) || (perFist == 3 && compFist == 2)) { System.out.println("结果:恭喜,你赢了!"); //用户赢 } else { System.out.println("结果说:你输了,真笨! "); //计算机赢 } }
//产生循环 count
++; System.out.println("是否进行下一轮:"); num=input.next().charAt(0); if(num=='n') { duo=false; } }while(duo); switch(number) { case 1:
System.out.println("刘备"+"vs"+name); break; case 2:
System.out.println("孙权"+"vs"+name); break; case 3:
System.out.println("曹操"+"vs"+name); break; } System.out.println("对战次数:"+count); System.out.println("结果说:你输了,真笨! "); } }
原文地址:https://www.cnblogs.com/liuying23/p/9789355.html