Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 爪哇:猪游戏变成无止境的循环?_Java - Fatal编程技术网

Java 爪哇:猪游戏变成无止境的循环?

Java 爪哇:猪游戏变成无止境的循环?,java,Java,所以我的编程课有一个任务,玩家和电脑玩猪的游戏,最多50分,多个类用来掷骰子。电脑在一个回合中最多可以玩到20点,玩家会被提示输入y或n以继续滚动或不滚动。如果任何骰子掷为1,则该回合的所有点数都将丢失 然而,游戏变成了一个无休止的循环,我不知道为什么。我对java相当陌生,所以我不知道还有什么别的说法 模具级 public class Die { private final int MAX = 6; //maximum face value private int f

所以我的编程课有一个任务,玩家和电脑玩猪的游戏,最多50分,多个类用来掷骰子。电脑在一个回合中最多可以玩到20点,玩家会被提示输入y或n以继续滚动或不滚动。如果任何骰子掷为1,则该回合的所有点数都将丢失

然而,游戏变成了一个无休止的循环,我不知道为什么。我对java相当陌生,所以我不知道还有什么别的说法

模具级

    public class Die {

    private final int MAX = 6; //maximum face value
    private int faceValue; //current value showing on the die

    public Die() {
        //Constuctor: Sets the initial face value
        faceValue = 1;

    }

    public int roll() {
        //Rolls the die and returns the result
        faceValue = (int)(Math.random() * MAX) + 1;
        return faceValue;

    }

    public void setFaceValue(int value) {
        //Face value mutator
        faceValue = value;

    }

    public int getFaceValue() {
        //Face value accessor
        return faceValue;

    }

    public String toString() {
        //Returns a string representation of this die.
        String result = Integer.toString(faceValue);
        return result;

    }
}
PairOfDice类

public class PairOfDice {
    //Creates two Die objects
    Die die1 = new Die();
    Die die2 = new Die();

    private int faceValue1;
    private int faceValue2;

    //Initialize sum variable as an integer
    int sum;

    public int getDie1() {
        //Get value of first die
        faceValue1 = die1.getFaceValue();
        return faceValue1;
    }

    public int getDie2() {
        //Get value of second die
        faceValue2 = die2.getFaceValue();
        return faceValue2;
    }

    public void setDie1(int value) {
        //Set value of first die
        die1.setFaceValue(1);
    }

    public void setDie2(int value2) {
        //Set value of second die
        die2.setFaceValue(1);
    }

    public int sumDice() {
        //Adds values of both dice into a sum
        sum = die1.getFaceValue() + die2.getFaceValue();
        return sum;
    }

    public int rollDice() {
        //Rolls both dice and returns results
        int roll = die1.roll() + die2.roll();
        return roll;
    }

    public String toString() {
        //Returns a string representation of the dice
        String result = Integer.toString(sum);
        return result;
    }
}
猪课/猪课(我的教授称之为猪游戏)

import java.util.Scanner;
公营猪{
公共静态void main(字符串[]args){
//声明数据变量
char-answer='n';
派洛夫丁;
模具模具1;
模具2;
Scanner playerAnswer=新扫描仪(System.in);
骰子=新的配对骰子();
die1=新模具();
die2=新模具();
int computerTotalScore=0;
int humanTotalScore=0;
国际计算机组织总数;
国际贸易总额;
int die1FaceValue=0;
int die2FaceValue=0;
//游戏的主循环
while(computerTotalScore<50和humanTotalScore<50){
//轮到计算机了
computerRoundTotal=0;//为计算机的下一轮重置
System.out.println(“当前状态:”);
System.out.println(“计算机:+computerTotalScore”);
System.out.println(“您:+humanTotalScore”);
而(computerRoundTotal<20&&(die1FaceValue>1&&die2FaceValue>1)){
die1.滚动();
die2.滚动();
die1FaceValue=die1.getFaceValue();
die2FaceValue=die2.getFaceValue();
系统输出打印项次(“模具1:+die1FaceValue+”,模具2:+die2FaceValue);
computerRoundTotal=computerRoundTotal+(die1FaceValue+die2FaceValue);
System.out.println(“当前轮:+computerRoundTotal”);
}
如果(die1FaceValue==1 | | die2FaceValue==1){
System.out.println(“Busted!”);
}
否则{
computerTotalScore=computerTotalScore+computerRoundTotal;
}
//轮到运动员了
humanRoundTotal=0;//为玩家的下一回合重置
System.out.println(“当前状态:”);
System.out.println(“计算机:+computerTotalScore”);
System.out.println(“您:+humanTotalScore”);
while(答案!=“y”&&(die1FaceValue>1 | | die2FaceValue>1)){
die1.滚动();
die2.滚动();
die1FaceValue=die1.getFaceValue();
die2FaceValue=die2.getFaceValue();
系统输出打印项次(“模具1:+die1FaceValue+”,模具2:+die2FaceValue);
humanRoundTotal=humanRoundTotal+(die1FaceValue+die2FaceValue);
System.out.println(“当前轮:+humanRoundTotal);
System.out.println(“再转一圈(是/否)”;
answer=playerAnswer.next().charAt(0);
}
如果(die1FaceValue==1 | | die2FaceValue==1){
System.out.println(“Busted!”);
}
否则{
humanTotalScore=humanTotalScore+humanRoundTotal;
}
}
如果(计算机总分>=50){
System.out.println(“计算机赢了!”);
}
否则{
System.out.println(“你赢了!”);
}
System.out.println(“最终结果:”);
System.out.println(“计算机:+computerTotalScore”);
System.out.println(“您:+humanTotalScore”);
}
}

我还没有调试您的整个代码,但是您无休止循环的原因是
die1FaceValue
die2FaceValue
被初始化为
0
。现在布尔表达式
computerRoundTotal<20&(die1FaceValue>1&&die2FaceValue>1)
(这也适用于玩家回合)将始终为
false
,因为
die1FaceValue==0
die2FaceValue==0
。要快速解决此问题,可以向while循环中添加if案例,如下所示

 while (computerRoundTotal < 20) {
   //roll your dice here

   if(die1FaceValue == 1 || die2FaceValue == 1){
     //reset points here and break, since the computers turn is over
     break;

   }
   //implement the "normal" turn logic here

}
while(computerRoundTotal<20){
//在这里掷骰子
如果(die1FaceValue==1 | | die2FaceValue==1){
//在这里重置点并断开,因为计算机已结束
打破
}
//在此执行“正常”转向逻辑
}
希望这有助于修复您的程序。请注意,我还没有检查您的代码是否有进一步的错误


您还可以检查
是否回答!='y'
。我不知道这是否是有意的,但玩家只有在输入
'n'
时才会继续滚动(我假设y代表是,n代表否)。因此,
answer==“y”
肯定是更合适的选择。

看看具体的情况,它在哪里变成了一个无休止的循环?请重新编写您的代码,以满足它成为一个您从未使用过的
PairOfDice
;既然现在是这样,根本没有必要把它包括在你的问题中。你应该用它吗?提示#2:while循环
while(答案!=“y”&&(die1FaceValue>1 | | die2FaceValue>1))
从不运行,因为
die1FaceValue
die2FaceValue
已初始化为0。
 while (computerRoundTotal < 20) {
   //roll your dice here

   if(die1FaceValue == 1 || die2FaceValue == 1){
     //reset points here and break, since the computers turn is over
     break;

   }
   //implement the "normal" turn logic here

}