我对编程很陌生,我一生都无法解决为什么收到错误消息:“ next(int)在java.util.Random中保护了访问”

这是发生错误的代码...

      /**
 * Generates Random Width Multiplier
 */
public static double randomRMultiplier() {
   Random randM = new Random();
   double randomMultiplier;

   //Calculates a random value between 12 and 20
   //(Divided by 10 to make a multiplier)
   do {
      switch(difficultyMark) {
         case 1:
           randomMultiplier = (randM.next((21-11) + 1) + 11); //Randoms between 11 and 21
           break;

         case 2:
           randomMultiplier = (randM.nextInt((20-12) + 1) + 12); //Randoms between 12 and 20
           break;

         case 3:
           randomMultiplier = (randM.nextInt((19-13) + 1) + 13); //Randoms between 12 and 20
           break;
        }
   } while ( randomMultiplier > 14 & randomMultiplier < 18); //Excludes 15,16,17 as they are too similar to golden values(16-->1.6)

   randomMultiplier = randomMultiplier / 10; //Divide by ten to make decimal multiplies

   return randomMultiplier;
}

最佳答案

我认为您不应该直接使用它。

它实际上是所有其他next*调用所使用的“实用程序”函数,如果您将Random替换为更合适的类(例如不是线性同余的类),则可以覆盖该函数。

根据您的代码,我建议您在打算使用next的地方不小心使用了nextInt

10-01 11:46