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