How do I generate multiple random variables in Java?

user14489288

I am making a game where the user gets to pick the number of die for each roll. Right now my code is processing the users input correctly, but it only randomly generates the last number, the other numbers are 0. (For example if the user input 4 the roll would appear : [0,0,0,3] Does anyone know how to get all the numbers to generate random numbers? Here is my main:

public class game
{
    public static void main(String[] args) 
  {
    int play = 1, sum = 0;
    int[] wins = new int[15];
    
    while ((play == 1) && (sum < 15)) 
    {
      sum = 0;;
      
      int roll = 0;
      Config die= new Config();
      //starts configuration
 
     
    die.hand();
    
      System.out.println("\nHere is your roll:\n");
      die.get();
    }
}

Here is my configure class:

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

class Config
{
    public static int Dice;
    int i;
    public void hand()
    {
        System.out.println("\nLet's Configure the Game...\n");
    
        String file = "Config.txt";
        Scanner scan = new Scanner(System.in);

        //configures number of dice in hand
        System.out.println("Enter the number of dice you would like in your hand (1-5): ");
        int hand_count = scan.nextInt();
        int[] Dice = new int[hand_count];
        System.out.println("Here is your roll: \n");
        Random random= new Random();
        
       for (i = 0; i<hand_count - 1; i++);
        { 
            Dice[i] = random.nextInt(6) + 1; 
            System.out.println(Arrays.toString(Dice)); 
        }
        

        System.out.println("\nNow lets play the game...\n"); 
    
//s.close();
    }
    public int get()
    {
        return Dice;
    }
}
sunrise
for (i = 0; i<hand_count - 1; i++);
        { 
            Dice[i] = random.nextInt(6) + 1; 
            System.out.println(Arrays.toString(Dice)); 
        }

You have a semi-colon at the end of the first line of this for loop, resulting in your loop having no contents. Get rid of the semi-colon, and move your opening curly brace onto the same line as the for loop, i.e.

for (i = 0; i<hand_count - 1; i++) {
  // do cool stuff
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I generate random sample data in my Oracle database?

How do I generate a random integer between min and max in Java?

How do I generate a random n digit integer in Java using the BigInteger class?

How do I generate a random password in a user's language?

How do I generate discrete random events with a Poisson distribution?

How do I make my Android app generate a random number?

How do I generate a random number in java between 450 and 150 that is a multiple of 10?

How do I generate random numbers in Dart?

How do I generate a random int number?

How do I generate random number for each row in a TSQL Select?

How do I generate random points in 3D space?

How do I generate a vector of random numbers in a range?

How do I generate a random timestamp in the next 5 days in Python?

How do I generate random doubles in an Array?

How do I generate a random num::BigUint?

How do I generate a random derangement of a list/array in java?

How do I generate random positions of hexagon using svg?

How do I generate a random color in a specific range?

How do I build random objects with multiple variables?

How do I generate random numbers to fill in empty rows?

How do I generate any random date between 01/01/2016 to 01/01/2017 using Java?

How do I generate random numbers x times?

How do I generate a primary key that is not random?

How can I generate random variables according to data types?

How do I generate a list with specified size of random integers within a range in Java 8?

Python: How do I generate a random matrix of 1.0 and 0.0, but with floats?

how do I generate random group names using lists

How do I generate a random integer in C#?

How do I generate a random number between 1 and 0 in SAS?