dimanche 21 octobre 2018

How to return the largest integer in an Array that has 10 random integers in it?

So this is a coding question from school I have, I don't want to say "hey guys do my homework for me!", I actually want to understand what's going on here. We just started on arrays and they kind of confuse me so I'm looking for some help. Here's the complete question:

Write a program in which the main method creates an array with 10 slots of type int. Assign to each slot a randomly-generated integer. Call a function, passing it the array. The called function should RETURN the largest integer in the array to your main method. Your main method should display the number returned. Use a Random object to generate integers. Create it with

Random r = new Random(7);

Generate a random integer with

x = r.nextInt();

So, here's what I have so far:

import java.util.Random;
public class Q1 {

public static void main(String[] args) { 
Random r = new Random(7);
int []count = new int[11];
int x = r.nextInt();
for (int i = 0; i < count.length; i++)
{
  count[i] = x;
}

I created that array with 10 ints, then used a for loop to assign each slot that randomly generated integer.

I'm having a hard time for what to do next, though. I'm not sure what kind of method / function to create and then how to go from there to get the largest int and return it.

Any help is really appreciated because I really want to understand what's going on here. Thank you!




Aucun commentaire:

Enregistrer un commentaire