简体   繁体   中英

Generate random integers with a range and place them into this array

I am working on a problem for 5 hours, and I searched in a book and on the Internet, but I still cannot solve this problem, so please help me to check what's wrong with the program. And the pic is the requirement for this program.

//imports
import java.util.Scanner;
import java.util.Random;

public class Lab09 // Class Defintion
{

    public static void main(String[] arugs)// Begin Main Method
    {

        // Local variables
        final int SIZE = 20; // Size of the array
        int integers[] = new int[SIZE]; // Reserve memory locations to store
                                        // integers

        int RanNum;
        Random generator = new Random();

        final char FLAG = 'N';
        char prompt;
        prompt = 'Y';

        Scanner scan = new Scanner(System.in);

        // while (prompt != FLAG);
        // {

        // Get letters from User
        for (int index = 0; index < SIZE; index++) // For loop to store letters
        {
            System.out.print("Please enter the number #" + (index + 1) + ": ");

            integers[index] = RanNum(1, 10);
        }

        // call the printStars method to print out the stars
        // printArray(int intergers, SIZE);

    } // End Main method

    /***** Method 1 Section ******/

    public static int RanNum(int index, int SIZE);

    {
        RanNum = generator.nextInt(10) + 1;

        return RanNum;
    } // End RanNum

    /***** Method 2 Section ******/

    public static void printArray(int integers, int SIZE) {

        // Print the result
        for (int index = SIZE - 1; index >= 0; index--) {
            System.out.print(integers[index] + "  ");
        }

    } // End print integers

} // End Lab09

As Tim Biegeleisen and Kayaman said, you should put everything in the question and not just an external image.

You have a lot of errors in your code. Below the code will compile and run but I recommend you to take a look and understand what it has been done.

Errors:

If you are declaring a method, make sure you use { at the end of the declaration. You have:

public static int RanNum(int index, int SIZE); 

Should be:

public static int RanNum(int index, int SIZE){
    // Code here
} 

You also should declare outside your main method so they can be accessed across the program.

If you are passing arrays as arguments, in your method the parameter should be an array type too.

You have:

public static void printArray(int integers, int SIZE) {
    // Code her
}

Should be

public static void printArray(int[] integers, int SIZE) {
    // Code her
}

Here is the complete code:

package test;

    import java.util.Random;
    import he java.util.Scanner;

    public class Test {
        //Local variables
        public static final int SIZE = 20; //Size of the array
        static int integers[] = new int[SIZE]; //Reserve memory locations to store integers
        static int randomNumber;
        static Random generator = new Random();
        static String prompt;
        static final String p = "yes";
        static boolean repeat = true;
        static Scanner input = new Scanner(System.in);

        Test() {
        }

        /*****  Method 1 Section ******/
        public static int RanNum (int low, int high) {
            randomNumber = generator.nextInt(high-low) + low;
            return randomNumber;
        } //End RanNum

        /*****  Method 2 Section ******/
        public static void printArray(int[] intArray, int SIZE) {   
            //Print the result
            for (int i = 0; i < SIZE; i++) {
                System.out.print (intArray[i] + "  ");
            }
         } //End print integers

        public static void main (String [] arugs) {
            do {
                for (int i = 0; i < SIZE; i++) {
                    integers[i] = RanNum(1, 10);
                }
                printArray(integers, SIZE);
                System.out.println("Do you want to generate another set of random numbers? Yes/No");
                prompt = input.nextLine();
            } while(prompt.equals(p));
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM