Skip to content
Advertisement

why is my method pickANumberPro() not working

I have created this program:

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

The program above runs, how can i convert this code above into a method, have main call it, and display the data of how many of each random number has been chosen? Below, is what i have started on.

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

edit:

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

Answer

This should fix your code.

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

Have fixed few things in code:

  1. Accept random object in pickANumberPro method
  2. print selectedNumber correctly
  3. Use scanner for getting keyboard input and use it
Advertisement