局数没有使用Tom类的属性调用,如果有会的,请指教一下
import java.util.Random;
import java.util.Scanner;
public class Homework014 {
//编写一个main方法
public static void main(String[] args) {
Tom t = new Tom();
int winTime = 0;
int[][] arr1 = new int[3][3];
int j = 0;
String[] arr2 = new String[3];
int countNum = 0;
//输入对象扫描器
Scanner myScanner = new Scanner(System.in);
for (int i = 0;i < 3 ;i++ ) {
//玩家出拳
System.out.println("请输入Tom要出的拳头 0-拳头,1-剪刀,2-布");
int num = myScanner.nextInt();
t.setTomNum(num);
int tomNum = t.getTomTactic();
arr1[i][j + 1] = tomNum;
//电脑出拳
int comNum = t.computerNum();
arr1[i][j + 2] = comNum;
//出拳作比较
String result = t.vsResult();
// arr1[i][j] = t.countNum; //如何自加?
//字符串输出
arr2[i] = result;
//计数器,没有使用属性
countNum ++;
arr1[i][j] = countNum;
//每单轮输出,这里和一维数组无关,并且基本调用Tom属性输出
System.out.println("=========================================");
System.out.println("局数\t玩家的出拳\t电脑的出拳\t输赢情况");
System.out.println(countNum + "\t" + tomNum + "\t\t" + comNum + "\t\t" + t.vsResult());
System.out.println("=========================================");
System.out.println("\n\n");
winTime = t.winCount(result);//记录赢的次数
}
//二维数组加单列数组输出
System.out.println("局数\t玩家的出拳\t电脑的出拳\t\t输赢情况");
for (int a = 0;a < arr1.length ;a++ ) {
for (int b = 0;b < arr1[a].length ;b++ ) {
System.out.print(arr1[a][b] + "\t\t");//前3行3列二位数组输出
}
System.out.print(arr2[a]);//一维数组的字符串使用,在最后一列
System.out.println();
}
System.out.println("共赢" + winTime + "局");
}
}
/*有人Tom设计他的成员变量。成员方法,可以电脑猜拳。戴
电脑每次都会随机生成0,1,2,
0表示石头,1表示剪刀,2表示布
并且可以显示Tom的输赢次数(清单)
*/
class Tom {
// int count = 1; //这里的计数器不知如何引用
int tomTactic;
int comTactic;
int winCountNum;
// 创建电脑的随机值
public int computerNum() {
Random r = new Random();
comTactic = r.nextInt(3);
return comTactic;
}
//Tom输入数据不再范围的情况
public void setTomNum(int tomTactic){
if (tomTactic < 0 || tomTactic > 2) {
throw new IllegalArgumentException("数字输入错误");
}
this.tomTactic = tomTactic;
}
//Tom输入数值的返回属性
public int getTomTactic() {
return tomTactic;
}
//与电脑比,返回字符串,准备输出的字符串
public String vsResult(){
if (tomTactic == 0 && comTactic == 1) {
return "Tom赢了";
} else if (tomTactic == 1 && comTactic == 2) {
return "Tom赢了";
} else if (tomTactic == 2 && comTactic == 0) {
return "Tom赢了";
} else if (tomTactic == comTactic) {
return "平局";
} else {
return "Tom输了";
}
}
//对Tom赢的情况进行计算,返回属性
public int winCount(String s){
if (s.equals("Tom赢了")) {
winCountNum ++;
}
return winCountNum;
}
}