首页 > 解决方案 > 为什么我的方法 pickANumberPro() 不起作用

问题描述

我创建了这个程序:

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]);
        
    }

上面的程序运行,我怎样才能把上面的代码转换成一个方法,让 main 调用它,并显示每个随机数选择了多少个数据?下面,是我开始的。

    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]);
    }

标签: javarandommethods

解决方案


这应该可以修复您的代码。

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. 正确打印 selectedNumber
  3. 使用扫描仪获取键盘输入并使用它

推荐阅读