当前位置: 首页 > 知识库问答 >
问题:

为什么我的方法pickANumberPro()不起作用

公羊英达
2023-03-14
import java.util.Random;
public class pickANumberPro {
    public static void main(String args[]) {
        
        //created the odds of each number being rolled
// 3 and seven have skewed odds to come up more often than the others
        String names[] = { "1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1", "2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2", "3","3","3","3","3", "4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4", "5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5", "6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","7", "8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10", };
//creates random object
        Random Dice = new Random(); 
//determines random
        int n = Dice.nextInt(166); 
prints out which number it lands on
        System.out.println(names[n]);
        
    }
    public static Double pickANumberPro(){
        String randomdie[] = { "1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1","1", "2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2","2", "3","3","3","3","3", "4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4","4", "5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5","5", "6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","6","7", "8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","8","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","9","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10","10"};
        Random dice = new Random(); 
        int n = dice.nextInt(166); 
        int rand = (randomdie[n]);
        return rand;
      //  System.out.println(rand[n]);
    }

    public static void main(String[] args)
    {
        final int ATTEMPTS = 500;
        int i, target, guess;  
        int wins;  // counter for number of wins (correct guesses) 
        Random rand = new Random();  // Create a Random object.

        // Create a Scanner object for keyboard input.
        Scanner keyboard = new Scanner(System.in);

        for (i=0, wins=0; i < ATTEMPTS; i++)
        {
            target = pickANumberPro(rand);
            guess = pickANumberPro(rand);
            if (target == guess) wins++;
        }
        System.out.printf( "Pro vs. Pro: %d wins out of %d attempts, %.2f%% \n", wins, ATTEMPTS, (wins*100.0/ (double)ATTEMPTS));

编辑:

 Random dice = new Random(); 
        int n = dice.nextInt(166); 
        String rand = (randomdie[n]);
        return rand;
      //  System.out.println(rand[n]);
    }

共有1个答案

越文康
2023-03-14

这将修复您的代码

public static void main(String[] args) {
    final int ATTEMPTS = 500;
    int i, target, guess;
    int wins;  // counter for number of wins (correct guesses)
    Random rand = new Random();  // Create a Random object.

    // Create a Scanner object for keyboard input.
    Scanner keyboard = new Scanner(System.in);

    for (i=0, wins=0; i < ATTEMPTS; i++)
    {
        guess = keyboard.nextInt();
        target = pickANumberPro(rand);
        if (target == guess) wins++;
    }
    System.out.printf( "Pro vs. Pro: %d wins out of %d attempts, %.2f%% \n", wins, ATTEMPTS, (wins*100.0/ (double)ATTEMPTS));
}

public static int pickANumberPro(Random random){
    int diceEntries[] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7, 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10};

    int n = random.nextInt(166);
    int selectedNumber = (diceEntries[n]);
    System.out.println(selectedNumber);

    return selectedNumber;
}

在代码中修正了一些内容:

  1. 在pickANumberPro方法中接受随机对象
  2. 正确打印所选编号
  3. 使用扫描仪获取键盘输入并使用
 类似资料:
  • 我了解碰撞检测的基础,并开发了我自己的碰撞检测方法(据我所知)。然而,这似乎并不奏效。我所做的不是围绕精灵、形状和其他对象绘制完整的矩形,而是简单地在对象的所有边(左、右、上、下)绘制线条(非常细的矩形)。 在指定子画面的左侧、右侧、顶部和底部绘制细矩形的方法。这可能不是进行碰撞的最佳方式,但这就是我所拥有的,并且对所有想法和不同的解决方案持开放态度! 注:(int)(x-Game.getCame

  • Stage.close()对我不起作用。 我查看了:JavaFX2.0:关闭一个舞台(窗口) 这是我的代码: 下面是调用消息框类的代码:

  • 我正在尝试检测我的两个精灵何时发生碰撞。我做的第一件事是在我的播放器周围创建一个矩形(称为player.img),然后在我想检测的树周围创建另一个矩形(称为背景.treesrect)。我将玩家矩形的坐标设置为等于当用户按下键移动时更新的坐标,但玩家矩形不移动。然后我使用精灵.碰撞(精灵)函数来检测它们是否碰撞并且没有检测到。有人可以向我展示为什么我的播放器矩形没有更新以及其他任何可能错误的内容吗?

  • 问题内容: 这是我所拥有的: 我是编程新手,所以如果这段代码看起来不成熟,我不会感到惊讶。无论如何,我让用户输入了一年零一个月(前三个字母)。我为a年创建了一个布尔变量,该变量表示用户输入的任何年份都需要被4、100和400整除。然后,我创建了一个if语句,以确认是否是a年才能打印出“ Feb(任何年份用户输入)中有DaysLeapYear。” 我认为我的算法有问题,因为如果我要取出TwentyE

  • 问题内容: 我在这里有点困惑。如果我将变量传递给json_decode,它将不起作用: 第一个回显正确显示了我传递的JSON字符串,例如 第二个回显显示NULL。因此,我从第一个回显中获取了字符串,并编写了以下代码: 你怎么说,它向我展示了正确解码的数组。字符串绝对相同,我什至保留转义字符。也许是问题所在? 问题答案: 看起来您的服务器已启用。无论是将其禁用或运行通过使用它之前。

  • 还不起作用。所以我放弃链接,我只是编码: