简体   繁体   English

生成具有范围的随机整数并将它们放入此数组中

[英]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.我在一个问题上工作了5个小时,在书上和网上搜索过,但我仍然无法解决这个问题,所以请帮我检查一下程序出了什么问题。 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.您还应该在 main 方法之外声明,以便可以在整个程序中访问它们。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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