Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to make a couple lines that checks if a random generated number is equal to a number of other integers if it's not, it returns that number, if not it repeats.

Here's the code:
Java
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);
    while(numFound = false)
    {
        if (holder == 0)
        {
            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;
            }
        }
    }


It should just print out 9 eventually. But, it just prints out random numbers like the while statement isn't even there. What's wrong?

What I have tried:

I've tried changing the
Java
Math.random()
method into the java.util.Random,
Java
Random rand = new Random(9)
Posted
Updated 10-Jul-18 13:49pm

"=" is an assignment, not a comparison:
while(numFound = false)
Try:
while(numFound == false)
 
Share this answer
 
Original Griff already answered your question. Let me improve your overall coding.

// all these sum, num, dum, can't you think something better? How about array?
static int sum1; // You are using default value of sum1; clever but bad practice; 
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);
    // even if you write the right syntax as OriginalGriff suggested. And your holder is not between 0-8; your function will stuck in an infinte loop. because holder is going to be always false
    while(numFound = false)
    {
        if (holder == 0)
        {
            holder = 1;
            if (holder!=sum1 && holder!=sum2 && holder!=sum3 && holder!=num1 && holder!=num2 && holder!=num3 && holder!=dum1 && holder!=dum2 && holder!=dum3)
            {
               // if your code reach here, you code will not reach else. common sense; but if you code do not reach here why do you think it will reach if of "else"?
                numFound = true;
            }
        } else {
            if (holder!=sum1 && holder!=sum2 && holder!=sum3 && holder!=num1 && holder!=num2 && holder!=num3 && holder!=dum1 && holder!=dum2 && holder!=dum3)
            {
                numFound = true;
            }
        }
    }


How to solve? fix few issues
1. How about instead of declaring each number, declare then as array
2. Move holder generation inside the while loop; so, if the if condition say false it will regenerate the number
3. to check whether number belongs to array, either write a function to check if the array contains your generated number or you can use the following suggestion, described in stackoverflow java - How can I test if an array contains a certain value? - Stack Overflow[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900