Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
        }

    }
like image 669
user1203 Avatar asked Feb 19 '23 08:02

user1203


1 Answers

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 =.

like image 98
OrangeDog Avatar answered Feb 27 '23 14:02

OrangeDog