mardi 10 juillet 2018

If/while statement doesn't execute (java)

I made a public int so that it will return a number different from a number of other integers. It should've returned 9, but, it just gives me a random number like the if statement isn't even there.

Here's the code

static int sum1;
static int sum2 = 1;
static int sum3 = 2;
static int num1 = 3;
static int num2 = 4;
static int num3 = 5;
static int dum1 = 6;
static int dum2 = 7;
static int dum3 = 8;


public static int getNumbers()
{
    int holder;
    boolean numFound = false;
    holder = 1 + (int) (Math.random() * 9);

    if (holder == 0) while(numFound = false)
    {
        holder = 1;
        if (holder!=sum1 && holder!=sum2 && holder!=sum3 && holder!=num1 && holder!=num2 && holder!=num3 && holder!=dum1 && holder!=dum2 && holder!=dum3)
        {
            numFound = true;
        }
    } else {
        if (holder!=sum1 && holder!=sum2 && holder!=sum3 && holder!=num1 && holder!=num2 && holder!=num3 && holder!=dum1 && holder!=dum2 && holder!=dum3)
        {
            numFound = true;
        }
    }
    return holder;
}
public static void main(String[] args) throws InterruptedException {
    sum1 = getNumbers();
    System.out.println(sum1);

}

What's happening and what can I do to fix it?

Aucun commentaire:

Enregistrer un commentaire