我在做练习的时候,发现输入同样的结果(我:0,电脑:1),但是结果不同,很疑惑
package p2;
import java.util.Random;
import java.util.Scanner;
public class Homework14 {
public static void main(String[] args) {
//写一个一维数组用来接收局数
int[] times = new int[10];
int inNum;
Tom t = new Tom();
Computer c = new Computer();
//写一个for循环遍历数组,i代表正在进行的局数
for (int i = 0; i < times.length; i++){
System.out.println("===共" + times.length + "局,第" + (i+1) + "局===");
System.out.println("0-石头 | 1-剪刀 | 2-布");
System.out.print("请输入:");
t.accept();//输入tom出的东西,传给tom的成员变量finger
int computerNum = c.outConNum();
System.out.println("电脑:" + computerNum);
c.compare(t);
t.showInfo();
}
System.out.println("对局结束");
}
}
class Tom{
int finger;//代表tom出得石头或剪刀或布
int winCount;//赢的次数
int lossCount;//输的次数
//写一个用来接受Tom输入的方法
public void accept(){
Scanner sc = new Scanner(System.in);
this.finger = sc.nextInt();
}
//在写一个赢的次数增加,输的次数增加的方法
public void add(){
this.winCount++;
}
//输的次数增加的方法
public void reduce(){
this.lossCount++;
}
//展示输赢次数的方法
public void showInfo(){
System.out.println("赢" + this.winCount + "局,输" + this.lossCount + "局\n");
}
}
class Computer{
//电脑需要出一个生成0,1,2三个数之间随机数的方法
public int outConNum(){
Random r = new Random();
return r.nextInt(3);
}
//再出一个比较电脑和tom的方法
public void compare(Tom t){
int computerNum = outConNum();
if (t.finger == 0){
if (computerNum == 1) {
t.add();
System.out.println("结果:you win");
} else if (computerNum == 2) {
t.reduce();
System.out.println("结果:you loss");
} else if (computerNum == 0) {
System.out.println("结果:平局");
}
} else if (t.finger == 1) {
if (computerNum == 2) {
t.add();
System.out.println("结果:you win");
} else if (computerNum == 0) {
t.reduce();
System.out.println("结果:you loss");
}else if (computerNum == 1) {
System.out.println("结果:平局");
}
} else if (t.finger == 2) {
if (computerNum == 0) {
t.add();
System.out.println("结果:you win");
} else if (computerNum == 1) {
t.reduce();
System.out.println("结果:you loss");
}else if (computerNum == 2) {
System.out.println("结果:平局");
}
}
}
}
我一开始认为conputerNum记录了上一次的,但发现这种思路也不对
我认真帮你修改了,加了注释,找Bug不易,还望博友采纳:
package cn.personal.demo09;
import java.util.Random;
import java.util.Scanner;
public class GameTest {
public static void main(String[] args) {
//写一个一维数组用来接收局数
int[] times = new int[10];
int inNum;
Tom t = new Tom();
Computer c = new Computer();
//写一个for循环遍历数组,i代表正在进行的局数
for (int i = 0; i < times.length; i++){
System.out.println("===共" + times.length + "局,第" + (i+1) + "局===");
System.out.println("0-石头 | 1-剪刀 | 2-布");
System.out.print("请输入:");
t.accept();//输入tom出的东西,传给tom的成员变量finger
//这两行代码放在这导致逻辑不对 int computerNum = c.outConNum();
//这两行代码放在这导致逻辑不对,将这句放在compare方法中的57行即可。 System.out.println("电脑:" + computerNum);
c.compare(t);
t.showInfo();
}
System.out.println("对局结束");
}
}
class Tom{
int finger;//代表tom出得石头或剪刀或布
int winCount;//赢的次数
int lossCount;//输的次数
//写一个用来接受Tom输入的方法
public void accept(){
Scanner sc = new Scanner(System.in);
this.finger = sc.nextInt();
}
//在写一个赢的次数增加,输的次数增加的方法
public void add(){
this.winCount++;
}
//输的次数增加的方法
public void reduce(){
this.lossCount++;
}
//展示输赢次数的方法
public void showInfo(){
System.out.println("赢" + this.winCount + "局,输" + this.lossCount + "局\n");
}
}
class Computer{
//电脑需要出一个生成0,1,2三个数之间随机数的方法
public int outConNum(){
Random r = new Random();
return r.nextInt(3);
}
//再出一个比较电脑和tom的方法
public void compare(Tom t){
int computerNum = outConNum();
System.out.println("电脑:" + computerNum);
if (t.finger == 0){
if (computerNum == 1) {
t.add();
System.out.println("结果:you win");
} else if (computerNum == 2) {
t.reduce();
System.out.println("结果:you loss");
} else if (computerNum == 0) {
System.out.println("结果:平局");
}
} else if (t.finger == 1) {
if (computerNum == 2) {
t.add();
System.out.println("结果:you win");
} else if (computerNum == 0) {
t.reduce();
System.out.println("结果:you loss");
}else if (computerNum == 1) {
System.out.println("结果:平局");
}
} else if (t.finger == 2) {
if (computerNum == 0) {
t.add();
System.out.println("结果:you win");
} else if (computerNum == 1) {
t.reduce();
System.out.println("结果:you loss");
}else if (computerNum == 2) {
System.out.println("结果:平局");
}
}
}
}
运行结果:
你调用了两次 outConNum(),打印的outConNum和比较时的 outConNum 不一定一样