This program generates 40 random numbers between 0 and 200 and stores them in an array. It then loops through the array to find the minimum value and prints it. The user is prompted to repeat the process or quit.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
27 views
Array Homework
This program generates 40 random numbers between 0 and 200 and stores them in an array. It then loops through the array to find the minimum value and prints it. The user is prompted to repeat the process or quit.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1
ArrayMinMax program
import java.util.Random; //import random scanner to generate the random
numbers import java.util.Scanner; //import scanner for user input public class ArrayMaxMin{ public static void main(String[] args) { //begin main method int size = 40; // initialise the variables , amount of numbers to be generated int max = 200; // maximum int[] array = new int[size]; // initialise new array of ints with 40 spaces int min = 1000; // min: a number greater than any random number that could be generated Random generator = new Random(); // initialise random Scanner input = new Scanner (System.in); // initialise scanner //Loop generates 40 integers and stores them in array using generator.nextInt(max) generator.nextInt(max); System.out.println ("Generating 40 random numbers..."); // print line for (int i =0; i<40; i++) // loop that will generate 40numbers. { array[i] =generator.nextInt(max); // add number generated into the array } for (int x =10; x <array.length;x++) // loop through the array { if (array[x] < min) // if number in array is less than min, set that number to min min = array[x]; System.out.println ("Minimum is: " + min); // print the minimum value System.out.println ("Would you like to do that again(Yes/No)?"); // ask user to repeat String word = input.next(); // user input is stored under String word if(word != "Yes") // if input doesnt equal to Yes, quit the program System.exit(0); } } //end main } //end program