人机猜拳

xiaoxiao2021-02-28  49

computer.java

public class Computer { String name; int score; public int showFirst() { int num = (int) Math.random() * 3 + 1; switch (num) { case 1: System.out.println("电脑出拳:剪刀"); break; case 2: System.out.println("电脑出拳:石头"); break; case 3: System.out.println("电脑出拳:布"); break; default: break; } return num; } }Person.java

import java.util.Scanner; public class Person { String name; int score; Scanner sc=new Scanner(System.in); public int showFirst() { System.out.println("请输入你的结果:1、剪刀 2、石头 3、布"); int num=sc.nextInt(); switch (num) { case 1: System.out.println("人的结果:剪刀"); break; case 2: System.out.println("人的结果:石头"); break; case 3: System.out.println("人的结果:布"); break; default: break; } return num; } } Game.java

import java.util.Scanner; public class Game { Person p = new Person(); Computer c = new Computer(); //Game g = new Game(); int count = 0; int win = 0; String isGo; public void start() { System.out.println("----------欢迎来到人机猜拳----------"); Scanner sc = new Scanner(System.in); System.out.println("请输入你的名字:"); p.name = sc.nextLine(); System.out.println("请选择你的对手:1、刘备 2、吕布 3、曹操"); int in = sc.nextInt(); switch (in) { case 1: System.out.println(p.name + "VS刘备"); break; case 2: System.out.println(p.name + "VS吕布"); break; case 3: System.out.println(p.name + "VS曹操"); break; default: System.out.println(); break; } do { int rePerson = p.showFirst(); int reCompu = c.showFirst(); if ((rePerson == 1 && reCompu == 3) || (rePerson == 2 && reCompu == 1) || (rePerson == 3 && reCompu == 2)) { System.out.println("恭喜你赢了"); p.score++; count++; win++; } else if (rePerson == reCompu) { System.out.println("平局"); count++; } else { System.out.println("你输了"); p.score--; count++; } System.out.println("是否继续y/n"); isGo = sc.next(); show(); } while (!isGo.equals("n")); } public void show() { System.out.println("一共对战了" + count + "局"); System.out.println("你胜利了" + win + "次"); } } Test.java

public class TestGuess { public static void main(String[] args) { Game g=new Game(); g.start(); } }

转载请注明原文地址: https://www.6miu.com/read-600084.html

最新回复(0)