3
import java.util.*;
import java.util.Arrays;
import java.util.Random;

class EZD_maxTester
{
  static int variables[] = new int[10];
  static int maximum()
 { 
     Random rand = new Random();

     int max = variables[0];

     for (int i = 0; i < 10; i++) 
     {
       variables[i] = rand.nextInt(100)+1;
     }

      for (int a = 0; a < variables.length; a++)
     {
       if (variables[a] > max)
       {
              max = variables[a];
       }
     }
     return max;
    }


 public static void main(String Args[])
  {
   int option;

   do{
   Scanner kbReader = new Scanner(System.in);
   System.out.println("Integers that will be compared: " + Arrays.toString(variables));
   System.out.println("");
   System.out.println("The maximum value is " + maximum());
   System.out.println("");
   System.out.println("Type 1 to run again. Type 0 to end the program.");
   option = kbReader.nextInt();

   while(option !=1 && option !=0)
   {

     if (option != 1 && option != 0)
     {
       System.out.println("Please enter 1 to run again or 0 to end the program.");
       option = kbReader.nextInt();
     }

   }

   System.out.println("");
   kbReader.close();

   }while(option==1);

 }
}

(i'm extremely new to coding so my code probably isn't as neat as it could be ! also this is my first time asking a question on here)

we're learning about arrays in class and i have to write a program that finds the largest integer in an array of randomly generated numbers, but when i run the program the array just prints as zeros yet it still gives me the largest value(??). it would print correctly when i used static int variables[] = new Random().ints(10, 0, 100).toArray();, but it would just print the same set of random numbers when i ran the program again. how do i make it print out the array without zeros masking my numbers?

bugjuice
  • 31
  • 7
  • I suggest that you learn how to debug your own code. Read https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ for some tips. – Code-Apprentice Apr 05 '18 at 23:55
  • 1
    Two side notes: You should create the `Scanner` just once before the loop instead of every time around the loop, and you should not `close()` it since it's wrapping `System.in`, and you didn't open that. The rule is, you should close anything you open, but ONLY things you open. – David Conrad Apr 06 '18 at 00:56

3 Answers3

3

You are calling System.out.println(Arrays.toString(variables)); before maximum() - the second actually sets the values in the array, thus they are all zero when you print. Move the print to after the call to maximum();

// System.out.println("Integers that will be compared: " + Arrays.toString(variables));
System.out.println("The maximum value is " + maximum());
System.out.println("Integers that were compared: " + Arrays.toString(variables));
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
2

When you call

System.out.println(Arrays.toString(variables));

you have not yet filled in the array.

The array is filled in in the maximum method.

A simplistic change would be to print the array after filling the array in

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

You need to call the maximum() method before this statement:

System.out.println("Integers that will be compared: " + Arrays.toString(variables));
PNS
  • 19,295
  • 32
  • 96
  • 143