ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

练习练习练习

练习练习练习 人机猜拳import java.util.Scanner;public class Qwe {public static void main(String[] args) {new Game().play();}}class User {private String name;private Scanner scanner new Scanner(System.in);private int choice;public User(String name) {this.name name;}public String getName() {return name;}public void game() {System.out.print(请出拳);choice scanner.nextInt();while (true) {if (choice 1) {System.out.println(你出拳剪刀);break;} else if (choice 2) {System.out.println(你出拳石头);break;} else if (choice 3) {System.out.println(你出拳布);break;} else {System.out.print(输入不合法请重新输入);choice scanner.nextInt();}}}public int getChoice() {return choice;}}class Computer {private int winCount;private int loseCount;private int total;private String fistName(int n) {if (n 1) return 剪刀;else if (n 2) return 石头;else return 布;}private int judge(int userChoice, int computerChoice) {if (userChoice computerChoice) return 0;if ((userChoice 1 computerChoice 3)|| (userChoice 2 computerChoice 1)|| (userChoice 3 computerChoice 2)) return 1;else return -1;}public void computerGame(User user, String role) {winCount 0;loseCount 0;total 0;Scanner scanner new Scanner(System.in);String again;do {total;user.game();int computerChoice (int) (Math.random() * 3) 1;System.out.println(role 出拳 fistName(computerChoice));int result judge(user.getChoice(), computerChoice);if (result 1) {System.out.println(恭喜你赢了);winCount;} else if (result -1) {System.out.println(很遗憾你输了);loseCount;} else {System.out.println(平局);}System.out.print(是否继续 y/n );again scanner.next();} while (y.equals(again));}public int getWinCount() { return winCount; }public int getLoseCount() { return loseCount; }public int getTotal() { return total; }}class Game {private Scanner scanner new Scanner(System.in);public void play() {System.out.println(******************************** 猜拳 ,开始 ********************************);System.out.println(出拳规则1.剪刀 2.石头 3.布);System.out.print(请输入你的名字);String userName scanner.next();User user new User(userName);String roundAgain;do {System.out.print(请选择对方的角色1.刘备 2.孙权 3.曹操);String role null;while (true) {int choice scanner.nextInt();if (choice 1) role 刘备;else if (choice 2) role 孙权;else if (choice 3) role 曹操;if (role ! null) {System.out.println(你选择了 role 对敌);break;}System.out.print(输入错误请重新输入);}System.out.print(要开始吗 y/n );String start scanner.next();if (!y.equals(start)) {System.out.println(已退出);break;}Computer computer new Computer();computer.computerGame(user, role);int userWin computer.getWinCount();int compWin computer.getLoseCount();int total computer.getTotal();if (userWin compWin) {System.out.println(userName VS role 比分为 userWin : compWin 对战场次 total 把 userName 胜利);} else if (compWin userWin) {System.out.println(userName VS role 比分为 userWin : compWin 对战场次 total 把 role 胜利);} else {System.out.println(userName VS role 比分为 userWin : compWin 对战场次 total 把平局);}System.out.print(是否开启下一轮 y/n );roundAgain scanner.next();} while (y.equals(roundAgain));System.out.println(游戏结束);}}怪物模块class Monster {String name;int blood;int attackPower;int defence;public void attack() { System.out.println(攻击); }public void move() { System.out.println(移动); }}class Goblin extends Monster {int rage;Overridepublic void attack() {System.out.println(哥布林正在用石头攻击);}Overridepublic void move() {System.out.println(哥布林缓慢移动);}}class CatDemon extends Monster {Overridepublic void attack() {System.out.println(猫妖正在用爪子攻击你);}Overridepublic void move() {System.out.println(猫妖跳着移动);}}抽象绘制abstract class All {String x;String y;String color;public abstract void draw();}class Line extends All {String finalCoordinate;String originalCoordinate;Overridepublic void draw() { System.out.println(绘制线条); }}class Rectangle extends All {int length;int width;Overridepublic void draw() { System.out.println(绘制矩形); }}class Circle extends All {int r;Overridepublic void draw() { System.out.println(绘制圆形); }}class Test1 {static public void testpage() {All[] a new All[3];a[0] new Line();a[1] new Circle();a[2] new Rectangle();for (int i 0; i a.length; i) {a[i].draw();}}}员工管理abstract class All {String x;String y;String color;public abstract void draw();}class Line extends All {String finalCoordinate;String originalCoordinate;Overridepublic void draw() { System.out.println(绘制线条); }}class Rectangle extends All {int length;int width;Overridepublic void draw() { System.out.println(绘制矩形); }}class Circle extends All {int r;Overridepublic void draw() { System.out.println(绘制圆形); }}class Test1 {static public void testpage() {All[] a new All[3];a[0] new Line();a[1] new Circle();a[2] new Rectangle();for (int i 0; i a.length; i) {a[i].draw();}}}Employees[] employees new Employees[100];for (int i 0; i employees.length; i) {if (Math.random() 0.5) {employees[i] new Manager();} else {employees[i] new Coder();}}for (Employees e : employees) {e.work();}面积形状class Shape {public double area() { return 0; }public void show() { System.out.println(这是一个形状); }}class Circles extends Shape {double radius;public Circles(double radius) { this.radius radius; }Overridepublic double area() { return Math.PI * radius * radius; }Overridepublic void show() { System.out.println(这是一个圆形); }}class Rectangles extends Shape {double length;double width;public Rectangles(double length, double width) {this.length length;this.width width;}Overridepublic void show() { System.out.println(这是一个矩形); }Overridepublic double area() { return length * width; }}class Test2 {static public void test2() {Scanner scanner new Scanner(System.in);System.out.println(请输入半径);double r scanner.nextDouble();System.out.println(请输入长和宽);double a scanner.nextDouble();double b scanner.nextDouble();Shape[] shape new Shape[2];shape[0] new Circles(r);shape[1] new Rectangles(a, b);for (int i 0; i shape.length; i) {System.out.println(shape[i].area());shape[i].show();}}}数组扩容class M {static public void array() {int i 0;Scanner scanner new Scanner(System.in);int[] arr new int[2];System.out.println(请输入数字-1结束);while (true) {int b scanner.nextInt();if (b -1) {System.out.println(结束输入);break;}arr[i] b;i;if (i arr.length) {arr Arrays.copyOf(arr, arr.length * 2);}}// 只打印有效部分for (int j 0; j i; j) {System.out.print(arr[j] );}}}学生管理class Student {private String name;private int age;private int score;public Student(int age, String name, int score) {this.age age;this.name name;if (score 0 || score 100) {System.out.println(成绩不合法);} else {this.score score;}}public int getAge() { return age; }public String getName() { return name; }public int getScore() { return score; }public void setAge(int age) { this.age age; }public void setName(String name) { this.name name; }public void setScore(int score) {if (score 0 || score 100) {System.out.println(成绩不合法);} else {this.score score;}}public void show() {System.out.println(姓名 name 年龄 age 成绩 score);}}
返回列表