public class Computer { String name; //名字 int score; //积分 //来个键盘录入 Scanner input =new Scanner(System.in); public void setName() { //电脑的名字要我们选择,所有同样有取名方法 System.out.println("请选择电脑的角色:1.吕布2.关羽3.秦始皇"); int choose =input.nextInt(); switch(choose) { case 1: name="吕布"; break; case 2: name="关羽"; break; case 3: name="秦始皇"; break; default: System.out.println("抱歉,没有这个选项"); } } public int chu() {//电脑也有出拳方法 //1~3之间的随机数 int choose =(int)(Math.random()*3+1); switch(choose) { case 1: System.out.println("电脑出拳:剪刀"); break; case 2: System.out.println("电脑出拳:石头"); break; case 3: System.out.println("电脑出拳:布"); break; } return choose; } } public class User { String name; //名字 int score; //积分 Scanner input =new Scanner(System.in);//首先来个键盘录入 public void setName() {//定义用户类中的取名方法 System.out.println("请输入你的名字:"); name =input.next(); } public int chu() { //定义用户类中的出拳方法 System.out.println("请出拳:1.剪刀2.石头3.布:"); int choose =input.nextInt(); switch(choose) { case 1: System.out.println("你出拳:剪刀"); break; case 2: System.out.println("你出拳:石头"); break; case 3: System.out.println("你出拳:布"); break; default: System.out.println("抱歉,没有这个选项"); } return choose; } } public class Game { Scanner input =new Scanner(System.in); User user; Computer computer; int count=0;//回合数 public void score() { user = new User(); computer = new Computer(); } public void start() {//游戏开始 System.out.println("\t\t欢迎来到猜拳小游戏\t\t"); System.out.println("\t\t*****************\t\t"); System.out.println("\t\t*****猜拳开始****\t\t"); System.out.println("\t\t*****************\t\t"); System.out.println(); System.out.println("猜拳规则:1.剪刀2.石头3.布"); System.out.println("请选择电脑的角色:1.吕布2.关羽3.秦始皇"); int choose =input.nextInt(); switch(choose) { case 1: computer.name="吕布"; break; case 2: computer.name="关羽"; break; case 3: computer.name="秦始皇"; break; default: System.out.println("抱歉,没有这个选择"); } System.out.println("请输入你的名字:"); user.name =input.next(); System.out.println(user.name+"vs"+computer.name+"\t对战开始"); int usechu;//用户出拳 int comchu;//电脑出拳 System.out.println("要开始吗:y/n"); String s =input.next(); while(s.equals("y")) { usechu =user.chu(); comchu=computer.chu(); if((usechu==1&&comchu==3) ||(usechu==2&&comchu==1) ||(usechu==3&&comchu==2)) { System.out.println("恭喜,本局你赢了"); user.score++;//用户积一分 }else if((usechu==1&&comchu==1) ||(usechu==2&&comchu==2) ||(usechu==3&&comchu==3)){ System.out.println("本局平局"); }else if((usechu==1&&comchu==2) ||(usechu==2&&comchu==3) ||(usechu==3&&comchu==1)) { System.out.println("你输了,渣渣"); computer.score++;//电脑积一分 } count++;//回合数+1 //输出每回合的用户积分 System.out.println("用户积分:"+user.score); //输出每回合的电脑积分 System.out.println("电脑积分:"+computer.score); System.out.println("第"+count+"回合\n");//输出回合数 //判断最终输赢并结束游戏 if(user.score==3) { System.out.println("最终结果:您获胜"); System.out.println("结束游戏"); break; //结束循环 }else if(computer.score==3) { System.out.println("最终结果:您输了"); System.out.println("结束游戏"); break; } System.out.println("\n是否开始下一轮:y/n"); s=input.next(); } while(s.equals("n")) { System.out.println("退出游戏"); break; } } } public class Test{ public static void main(String[] args) { Game game =new Game(); game.score(); game.start(); } }
|