-1

I have a problem with my code, it's a simple dice game:

import java.util.Random;
class Zaruri {
    public static void main(String args[]) {
        Random roll = new Random();
        int[] zar = new int[2];

            for (int i = 0; i < 1; i++)  {
                for(int k = 0; k < 2; k++){
                zar[i] = (int) (roll* 6) + 1;
            }
            if (zar[0] == zar[1]) {
                System.out.println("Your numbers are : " + zar[0] + " and " + zar[1]  + "\nYou won! \nYEEEY!!");

            } else {
                System.out.println("Your numbers are : " + zar[0] + " and " + zar[1]  + "\nYou lost, better luck next time!");
            }
            }
    }
}

I don't know how to make it work,first it won't let print out the second dice(it comes allways 0) and if i put more then 1 roll and (roll.nextDouble * 6) + 1; it will work,but i don't want more then 1 roll. CAn you help me ? What am i doing wrong??

RoToRa
  • 37,635
  • 12
  • 69
  • 105
Victor
  • 468
  • 1
  • 5
  • 17

6 Answers6

5

You don't need 2 for loops. Just 1 loop will do. Remove the outer i loop and let k loop do your random int generation.

for (int k = 0; k < 2; k++) {
    zar[k] = roll.nextInt(6) + 1; // k loop populates your array.
}
if (zar[0] == zar[1]) {
    System.out.println("Your numbers are : " + zar[0] + " and "
            + zar[1] + "\nYou won! \nYEEEY!!");

} else {
    System.out.println("Your numbers are : " + zar[0] + " and "
            + zar[1] + "\nYou lost, better luck next time!");
}
Rahul
  • 44,383
  • 11
  • 84
  • 103
1

There are a number of issues, i'll go through them 1 by 1

    for (int i = 0; i < 1; i++)  {
        for(int k = 0; k < 2; k++){
            zar[i] = (int) (roll* 6) + 1;
        }
        <snip rest of loop>

The loop with k in does nothing but repeat the line zar[i] = (int) (roll* 6) + 1; several times, it does nothing, this loop should be removed


        for (int i = 0; i < 1; i++)  {
            for(int k = 0; k < 2; k++){
                zar[i] = (int) (roll* 6) + 1;
            }
            if (zar[0] == zar[1]) {
                System.out.println("Your numbers are : " + zar[0] + " and " + zar[1]  + "\nYou won! \nYEEEY!!");

            } else {
                System.out.println("Your numbers are : " + zar[0] + " and " + zar[1]  + "\nYou lost, better luck next time!");
            }
        }

The print statements are within the for loop, so it prints out every time through the loop, the first time through the loop only the first die will be set, the second time only the first two, etc etc.


zar[i] = (int) (roll* 6) + 1;

roll is of class random, it is not a number but generates random numbers, this should be

zar[i] = (int) (roll.nextDouble()* 6) + 1;

or more sensibly (as you want an int in the end anyway

zar[i] = roll.nextInt()+ 1;


for (int i = 0; i < 1; i++)

Numbers from i=0 to i<1 is just one number, 0, dice 1 is never set because its only goes through the loop once

Richard Tingle
  • 16,906
  • 5
  • 52
  • 77
0

Try to use Math.random() instead of Random()

Gilles V.
  • 1,412
  • 13
  • 20
0

I don't see why you have two loops, and especially the one with k as an index. You are not using that k anywhere. Try this:

 for (int i = 0; i < 2; i++)  {
     zar[i] = roll.nextInt(6) + 1;
 }
darijan
  • 9,725
  • 25
  • 38
0

try this instead :

int Low = 10;
int High = 100;
int R = r.nextInt(High-Low) + Low;

SOURCE : How can I generate random number in specific range in Android?

or Java Generate Random Number Between Two Given Values

Community
  • 1
  • 1
wazaminator
  • 245
  • 3
  • 15
0

Delete the first loop with the i and let only the k loop. Then it won't logically work and always print 0 because you don't generate a random number. You must do zar[i]=roll.nextInt(6)+1; because the range is from 0 (included) and 6 (excluded). Logically you must add 1 whatever will be the result to obtain a 1-6 range.

Federico
  • 521
  • 5
  • 13