第四周java学习
程序员文章站
2022-09-24 19:29:53
猜数字游戏玩家类:package com.softeem.lesson15.test.guessnum;/** * 玩家类 * @author mrchai */public class Player {private int id;private String name;public Player() {}public Player(int id, String name) {super();this.id = id;this.name...
猜数字游戏
玩家类:
package com.softeem.lesson15.test.guessnum;
/**
* 玩家类
* @author mrchai
*/
public class Player {
private int id;
private String name;
public Player() {
}
public Player(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Player [id=" + id + ", name=" + name + "]";
}
}
比赛场次类:
package com.softeem.lesson15.test.guessnum;
//软件 = 数据结构 + 算法
/**
* 比赛场次类
* @author mrchai
*/
public class Match {
/**比赛场次*/
private int num;
/**比赛名称*/
private String name;
/**获胜方*/
private Player winner;
/**失败方*/
private Player loser;
/**获胜方猜的数字*/
private int winnerNum;
/**失败方猜的数字*/
private int loserNum;
/**判断是否平局*/
private boolean equal;
public Match() {
}
public Match(int num, String name, boolean equal) {
super();
this.num = num;
this.name = name;
this.equal = equal;
}
public Match(int num, String name, Player winner, Player loser, int winnerNum, int loserNum) {
super();
this.num = num;
this.name = name;
this.winner = winner;
this.loser = loser;
this.winnerNum = winnerNum;
this.loserNum = loserNum;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Player getWinner() {
return winner;
}
public void setWinner(Player winner) {
this.winner = winner;
}
public Player getLoser() {
return loser;
}
public void setLoser(Player loser) {
this.loser = loser;
}
public int getWinnerNum() {
return winnerNum;
}
public void setWinnerNum(int winnerNum) {
this.winnerNum = winnerNum;
}
public int getLoserNum() {
return loserNum;
}
public void setLoserNum(int loserNum) {
this.loserNum = loserNum;
}
public boolean isEqual() {
return equal;
}
public void setEqual(boolean equal) {
this.equal = equal;
}
@Override
public String toString() {
return "Match [num=" + num + ", name=" + name + ", winner=" + winner + ", loser=" + loser + ", winnerNum="
+ winnerNum + ", loserNum=" + loserNum + ", equal=" + equal + "]";
}
}
package com.softeem.lesson15.test.guessnum;
import java.util.ArrayList;
import java.util.Scanner;
public class GuessNumberGame {
public ArrayList<Match> start() {
//声明比赛场次的集合,用于存储每一次比赛结果
ArrayList<Match> list = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("请输入要猜数字的次数:");
//接收需要猜数字的次数
int count = sc.nextInt();
//创建玩家对象
Player p1 = new Player(1, "杰克");
Player p2 = new Player(2, "肉丝");
int num = 0;
//只要比赛剩余次数大于0则继续比赛
while(count > 0) {
//记录场次
num++;
//随机生成两个整数分别代表玩家1和2 猜的数字
int n1 = (int)(Math.random() * 5) + 1;
int n2 = (int)(Math.random() * 5) + 1;
//声明比赛场次引用
Match m = null;
if(n1 > n2 || (n1==1 && n2 == 5)) {
//n1赢
m = new Match(num, "猜数字游戏", p1, p2, n1 , n2);
}else if(n2 > n1 || (n2 == 1 && n1 == 5)) {
//n2赢
m = new Match(num, "猜数字游戏", p2, p1, n2 , n1);
}else {
//平局
m = new Match(num, "猜数字游戏", true);
}
//将比赛结果存储到集合
list.add(m);
count--;
}
return list;
}
public static void main(String[] args) {
ArrayList<Match> list = new GuessNumberGame().start();
for (Match m : list) {
System.out.println(m);
}
}
}
本文地址:https://blog.csdn.net/Kennys_J/article/details/109963112
上一篇: JavaEE课程设计:数据库的增删改查之添加页(图书)
下一篇: Java语法+=常量和变量的运算