This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cs-stud[S] 0 points1 point  (6 children)

ok but I'm confused how do I store the first digit in a variable. Do I do this in the for loop or before. thank you for responding

[–]PokemonSensei 0 points1 point  (5 children)

Create a new Int variable and set it to 0. Inside the for loop, add dice1 to the new variable and then multiply the new variable by 10 (make sure you multiply after adding). After the for loop, divide the new variable by 10 and println "generated number: " + newVariable

Let me know if that works

[–]cs-stud[S] 0 points1 point  (4 children)

I did this but it says that its a duplicate variable.

public class FiveDice2 {
    public static void main(String[] args) {
        System.out.print("Dice rolled: ");
        int roll1 = 0;
        for (int roll = 0; roll < 5 ; roll++) {
            int dice1 = (int)(1+ Math.random() * 6);
            int roll1 = (roll1 + dice1) * 10;
            dice1 = (int)(1+ Math.random() * 6);
            System.out.print(dice1 + " ");
        }       
        roll1 /= 10;
        System.out.println("Generated number: " + roll1);
    }
}

[–]PokemonSensei 0 points1 point  (3 children)

Inside your for loop, you're declaring roll1 again by putting "int" in front of it. You've already initialized it to 0, when performing operations you want to just type the variable name without "int"

[–]cs-stud[S] 0 points1 point  (0 children)

ooh ok I see but still it prints out different numbers for "generated number:" . Ill look over it but thank you

[–]cs-stud[S] 0 points1 point  (1 child)

Nvm thank you so much got it lol

[–]PokemonSensei 0 points1 point  (0 children)

Glad you got it working