Reputation: 3
-- I am to write a program that prompts for N which is number of dice to roll and M for number of times to roll. I must repeat M times N6 or six sided die and compute and record the total sum of rolls. Using an array i must report how many times along with a percentage each possible total from 6 to 6N occurred.
Here is my code so far, i cannot get it to compile, and i think im going about it completely wrong, we only have one professor that teaches java, and he is not good at explaining things and always seems to be in a hurry if we ask questions. This is my second division class, and i learned barely anything the first semester. ////////////////////////////////
import java.util.Random;
import java.util.Scanner;
public class Lab1
{
public static Scanner in = new Scanner (System.in);
public static void main (String[] args)
{
int dice = 0;
int roll = 0;
while (true)
{
System.out.print ("How many dice do you roll?");
dice = in.nextInt();
}
System.out.print ("How many Times do you want to roll?");
roll = in.nextInt();
}
int dicetotal = Dicecount (dice); //Error message. dice cannot be resolved to Variable//
for (int i = 0; i< roll; i++)
System.out.println (Dicecount (dice));
}
}
public static int Dicecount ( int dice);
{
int dicetotal = 0;
for (int x = 0: x < dice; x++)
{
int rollcount = (int) (1+6* (Math.random()));
dicetotal+=rollcount;}
return dicetotal;
}
}
Upvotes: 0
Views: 1169
Reputation: 1970
Two other problems this code appears to have:
1) In Java method names should always start with a lower-case letter. Here you have a method called dicecount
. This is an invalid name and will confuse the Java compiler.
2) On the line where you declare the Dicecount method, you have a semicolon right at the end. This is a syntax error and will cause your code not to compile or behave incorrectly. The reason for this is that the semicolon is essentially telling the compiler that the current line is a complete statement. But it isn't finished, you still need to declare the body of the method.
So my advice is to change this line public static int Dicecount ( int dice);
to read like this public static int dicecount(int dice)
. That is, remove the leading capital letter, and get rid of the semicolon at the end.
Upvotes: 0
Reputation: 28687
Properly format your code. This will help you find that the 6 lines starting with:
int dicetotal = Dicecount (dice);
Are not within a function block, and need to be.
You also have a colon instead of a semi-colon in this line (~7th from the bottom):
for(int x = 0; x < dice; x++){
Fixing these errors will allow your code to successfully compile - but that doesn't mean that it will do what you want it to do. Since this is homework, you'll be expected to find these issues and at least do initial troubleshooting on them yourself.
Upvotes: 1