且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

方法不能应用于给定的类型

更新时间:2022-06-23 16:52:34

for(int i = 0; i < 100000; i++){
 d.throwDice();
  if(d.throwDice() == diceScore){
    scoreCount += 1;
  }
}

此代码有两点错误:

  1. 它调用不带intthrowDice(您已将其定义为public int throwDice(int diceCount),因此必须给它一个int)
  2. 每个循环调用throwDice两次
  1. It calls throwDice without an int (you have defined it as public int throwDice(int diceCount), so you must give it an int)
  2. It calls throwDice twice each loop

您可以这样解决它:

for(int i = 0; i < 100000; i++){
 int diceResult = d.throwDice(diceCount); // call it with your "diceCount"
                                          // variable
  if(diceResult == diceScore){ // don't call "throwDice()" again here
    scoreCount += 1;
  }
}