user1203
user1203

Reputation: 43

While Loops Bypassed?

So I'm a first year comp sci student, and I have to create a program which takes an array scrambles it, and then sorts it again. I've pretty much got it working, but for some reason, one of my while loops will not work and is completely bypassed. I'm really new at this, so all help is appreciated. I have sort, and print working, scramble is just giving me some trouble.

import java.io.*;
import java.util.Random;
public class Driver03
    {
     public static void main(String[] args)
      {
        int[] array = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109};
        print(array);
        array = scramble(array);
        print(array);
        array = sort(array);
        print(array);
      }
      public static void print(int[] apple)
        {
            for(int x = 0; x < apple.length; x++){
                System.out.println(apple[x]+"");
                }
        }
      public static int max;
      public static int maxIndex;
      public static int temp;
      public static int[] sort(int[] grape)
        {
            for(int y = grape.length; y > 0; y--){
            for(int x = 0; x < y; x++)
              if (x==1){
                    if (grape[x] > grape[x-1]){
                        max = grape[x];
                        maxIndex = x;
                        }
                    else{
                        max = grape[x-1];
                        maxIndex = x - 1;
                        }
                    }
                 else{
                    if (grape[x] > max){
                        max = grape[x];
                        maxIndex = x;
                        }
                    }
                 temp = grape[maxIndex];
                 grape[maxIndex] = grape[y-1];
                 grape[y-1] = temp; 
                }
            return grape;
        }
     public static int temp1;
     public static boolean t;
     public static boolean u;
     public static int[] numbers;
     public static int[] tangerine;
     public static int[] scramble(int[] orange)
        {
        numbers = new int[10];
        tangerine = new int[10];
        Random rand=new Random();

        for(int x = 0; x < orange.length; x++){
            t=false;
            while (t=false){ //For some reason being bypassed
                int randn = rand.nextInt(9);
                for(int y = 0; y < numbers.length; y++){
                    if (numbers[x]==randn)
                        u = true;
                    }
                if (! (u==true))
                    {
                    numbers[randn] = randn;
                    tangerine[randn] = orange[x];
                    t = true;
                    }
                }           
            }
          return tangerine;
        }

    }

Upvotes: 4

Views: 164

Answers (3)

jahroy
jahroy

Reputation: 22692

One equal sign: x = 4 is used to assign a value to a variable.

Two equal signs: x == 6 is used to test if a variable is equal to a value.


Some of the syntax in your if statements is unorthodoxed.

This is very bizarre

if (! (u==true))

You can use this instead:

if (!u)

Better yet, you can use a descriptive variable name and make it much more readable:

if (!found) {
    shuffleItems();
}

Note how readable that is: If not found, shuffle items.

Now your code documents itself!

Upvotes: 2

OrangeDog
OrangeDog

Reputation: 38777

t=false assigned the value false to t and evaluates to the new value. Thus you've just written while (false).

When comparing equality you need to use ==.

However, in the case of booleans, it's much better style to just use while (!t). This is both more readable and less likely to go wrong due to missing a second =.

Upvotes: 6

Parallelis
Parallelis

Reputation: 719

use:

while (! t){

And remember to use == to compare value :)

Upvotes: 0

Related Questions