简体   繁体   中英

java code not compiling without any error

This code is not working. What could be wrong? I'm new and I am not able to understand what is wrong. I'm compiling using an app. The code runs but there is no printout there. What should I do.

public class Main {
    public static void main(String[] args) {
        /*we will make a game in which we try to get same number on all of our dice. And count how much tries it take to match the dice.
        */

        int a = keepRolling();
        System.out.println(a);

    }

    //creating dice function first
    public static int rollDice() {
        double randomNumber = Math.random();
        randomNumber = randomNumber * 6;
        int randomAccurate = (int) randomNumber;
        randomAccurate = randomAccurate + 1;
        return randomAccurate;

    }


    public static int keepRolling() {
        int roll1 = rollDice();
        int roll2 = rollDice();
        int count = 1; //one time is already rolled when the function is called
        while (!(roll1 == roll2)) {
            int roll1 = rollDice();
            int roll2 = rollDice();
            count = count + 1;
        }
        return count;
    }

}

It might take a long time to print it (you created a kind of infinite loop). The chances of condition

 while (!(roll1 == roll2)) is very rare to be false.

Because both roll1 and roll2 have random numbers and being them equal is rare and time consuming. This is why your program will keep on running (or might give stackoverflow exception) and in very rare occassions might print the count.

Unrelated: you should have used

while(roll1 != roll2) instead of while (!(roll1 == roll2)) 

and not create new variable under the loop (use previous variables declared at method start)

You are usign new variables in the loop, roll1 and roll2 never change, it should be

public static int keepRolling() {
    int roll1 = rollDice();
    int roll2 = rollDice();
    int count = 1; //one time is already rolled when the function is called
    while (!(roll1 == roll2)) {
        roll1 = rollDice();
        roll2 = rollDice();
        count = count + 1;
    }
    return count;
}

rollDice,take care this method. it's build with Math.random() you never know what number it's,which means if you wanna see print something,it could be forever.

You could also do this

    public static int keepRolling() {
        int dice1, dice2, count = 0; 
        do {
            dice1 = rollDice();
            dice2 = rollDice();
            count++;
        } while (dice1 != dice2);
        return count;
    }

roll1 and roll2 is completely removed in this case

buddy;

change

 while (!(roll1 == roll2)) {
            int dice1 = rollDice();
            int dice2 = rollDice();
            count = count + 1;
        }

to

 while (!(roll1 == roll2)) {
            int roll1 = rollDice();
            int roll2 = rollDice();
            count = count + 1;
        }

restart code; you can see that;

your code has wrong on:

while (!(roll1 == roll2))

this condition aways return true; becuse roll1 and roll2 never change;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM