java简单项目:人机猜拳

人机猜拳

  1. 开始猜拳游戏后,先选择计算机角色
  2. 接着输入玩家姓名,开始游戏
  3. 与计算机猜拳,统计猜拳胜负记录,胜方每次加一分
  4. 结束后显示对战次数,并根据分数显示相应结果

User 类- - - 用户的出拳逻辑

package com.fingerguess.test;

import java.util.Scanner;

public class User {
		static Scanner sc = new Scanner(System.in);
		public static String name = "";
		public static int score=0;
		
		//出拳方法
		public static int finger() {
			System.out.println("请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
			int data = sc.nextInt();
			switch(data) {
				case 1:
					System.out.println("你出拳:剪刀");
					break;
				case 2:
					System.out.println("你出拳:石头");
					break;
				case 3:
					System.out.println("你出拳:布");
					break;
				default:
					System.out.println("输入错误!");
					break;
			}
			return data;
		}	
	}

Computer 类- - -计算机的出拳逻辑

package com.fingerguess.test;

public class Computer {
		public static String name = "计算机";
		public static int score = 0;
		
		//出拳方法
			public static int finger() {
				int data = (int)(Math.random()*3+1);
				switch(data) {
					case 1:
						System.out.println(name+"出拳:剪刀");
						break;
					case 2:
						System.out.println(name+"出拳:石头");
						break;
					case 3:
						System.out.println(name+"出拳:布");
						break;
				}
				return data;
				
			}
	}




Game 类- - -猜拳界面类

package com.fingerguess.test;

import java.util.Scanner;

public class Game {
		static Scanner sc = new Scanner(System.in);
		User user;
		Computer computer;
		static int fightFrequency=0;

		//初始化方法
		public static void initial() {
			System.out.println("----------------欢迎进入游戏世界----------------");
			System.out.println("                  ************");
			System.out.print("                  **猜拳开始**");
			System.out.print("\n                  ************");
			System.out.println("\n出拳规则:1.剪刀 2.石头 3.布");
		}
		//开始游戏方法
		public static void startGame() {
			System.out.printf("请选择计算机角色:1.刘备 2.曹操 3.孙权:");
			int answer = sc.nextInt();
			System.out.println("请输入你的姓名:");
			User.name = sc.next();
			switch(answer) {
				case 1:
					Computer.name = "刘备";
					System.out.println(User.name+" VS "+Computer.name);
					break;
				case 2:
					Computer.name = "曹操";
					System.out.println(User.name+" VS "+Computer.name);
					break;
				case 3:
					Computer.name = "孙权";
					System.out.println(User.name+" VS "+Computer.name);
					break;
				default:
					System.out.println("无此人物选项!");
					break;
			}
		}
		//第一次游戏方法
		public static void showFirst() {
			initial();
			startGame();
			int userNum = User.finger();
			int computerNum = Computer.finger();
			calResult(userNum, computerNum);
			
			while(true) {
				System.out.println("是否开始下一轮?(y/n):");
				String answer = sc.next();
				if(answer.equalsIgnoreCase("y")) {
					int userNum1 = User.finger();
					int computerNum1 = Computer.finger();
					calResult(userNum1, computerNum1);
				}else {
					break;
				}
			}
			
		}
		//计算结果方法
		public static void calResult(int userNum,int computerNum) {
			if((userNum==1&&computerNum==3)||(userNum==2&&computerNum==1)||(userNum==3&&computerNum==2)) {
				User.score++;
				System.out.println("结果说:^_^你赢了!");
			}else if((userNum==1&&computerNum==2)||(userNum==2&&computerNum==3)||(userNum==3&&computerNum==1)){
				Computer.score++;
				System.out.println("结果说:^_^你输了!");
			}else {
				System.out.println("结果说:^_^竟然是平局!!");
			}
			fightFrequency++;
		}
		//显示最终游戏结果方法
		public static void showResult() {
			System.out.println(Computer.name+":"+Computer.score+" VS "+User.score+":"+User.name);
			System.out.println("对战次数:"+fightFrequency);
			if(Computer.score>User.score) {
				System.out.println("你输了,还需要加油");
			}else if(Computer.score<User.score) {
				System.out.println("电脑都赢不了你了!");
			}else {
				System.out.println("你和电脑平局");
			}
		}
		
		
		//主运行方法
		public static void main(String[] args) {
			showFirst();
			showResult();
		}
	}

你可能感兴趣的:(java简单项目:人机猜拳)