用Java写代码,1.分析业务:抽象出类;描述出类的行为和特征; 用户类(User) 计算机类(Computer) 属性: 名字(初始值为匿名): u_name 积分(初始值 0):u_score 名字(初始值为匿名): c_name 积分(初始值 0):c_score 方法: 出拳 chuQuan(int choice) 出拳 chuQuan(int choice) 2.创建游戏用户类(User)和计算机类(Computer) 3.创建游戏类(Game): 编写它的方法:初始化 init(),计算对战结果 calBattleResult(),显示结果 showResult ()。 属性:甲方玩家(用户)、乙方玩家(计算机)、对战次数(battleCount)。 4.编写游戏类的方法:开始游戏 beginGame() 5.编写测试类
时间: 2024-01-15 20:03:15 浏览: 30
可以的,您的问题是如何创建游戏用户类(User)和计算机类(Computer)吗?那么我将给您提供一些参考代码:
用户类(User):
```java
public class User {
private String u_name;
private int u_score;
public User() {
this.u_name = "匿名";
this.u_score = 0;
}
public User(String u_name, int u_score) {
this.u_name = u_name;
this.u_score = u_score;
}
public String getU_name() {
return u_name;
}
public void setU_name(String u_name) {
this.u_name = u_name;
}
public int getU_score() {
return u_score;
}
public void setU_score(int u_score) {
this.u_score = u_score;
}
public int chuQuan(int choice) {
// 根据用户输入的选项进行出拳操作
// 返回出拳的结果
}
}
```
计算机类(Computer):
```java
public class Computer {
private String c_name;
private int c_score;
public Computer() {
this.c_name = "匿名";
this.c_score = 0;
}
public Computer(String c_name, int c_score) {
this.c_name = c_name;
this.c_score = c_score;
}
public String getC_name() {
return c_name;
}
public void setC_name(String c_name) {
this.c_name = c_name;
}
public int getC_score() {
return c_score;
}
public void setC_score(int c_score) {
this.c_score = c_score;
}
public int chuQuan() {
// 计算机进行出拳操作
// 返回出拳的结果
}
}
```
这两个类是游戏类(Game)中的属性,而游戏类的初始化方法(init())、计算对战结果方法(calBattleResult())和显示结果方法(showResult())需要您自行编写。然后,您可以编写开始游戏方法(beginGame())和测试类。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)