将值放入数组槽时,为什么会出现“类预期”错误?

时间:2023-01-20 16:45:11

I'm new to this website so I barely a clue on how to use it, I tried, I'm sorry if I've made mistakes. My program intakes ten integer values and places them in an array, then displays the contents of the array in reverse. I keep receiving an error that states:

我是这个网站的新手,所以我几乎不知道如何使用它,我试过,如果我犯了错误,我很抱歉。我的程序插入十个整数值并将它们放在一个数组中,然后反向显示数组的内容。我一直收到一条错误,指出:

ReverseIntegers.java:23: class expected

ReverseIntegers.java:23:预期的类

Here's the code that it says is having the issue:

以下是它所说的代码有问题:

IntValues[number1] = int.parseInt (UserInput)

IntValues [number1] = int.parseInt(UserInput)

And here is the full code I've written:

以下是我写的完整代码:

class ReverseIntegers
{
    public static void main (String[] args) throws IOException
    {
        String UserInput, Integers;
        int number1 = 9;
        int[] IntValues = new int[10];
        InputStreamReader inStream = new InputStreamReader (System.in);
        BufferedReader num = new BufferedReader (inStream);
        System.out.println ("Enter ten integer values and the program will display them in reverse order.");
        while (number1 > -1)
        {
            UserInput = num.readLine();
            IntValues[number1] = int.parseInt (UserInput);
            System.out.println (number1 + " numbers are left to be entered, please continue.");
            number1--;
        }
        number1 = 0;
        System.out.println ("The numbers entered in reverse order are: ");
        while (number1 < 10)
        {
            System.out.println (IntValues[number1]);
            number1++;
        }
    }
}

Help! I don't know what the problem is!

救命!我不知道是什么问题!

1 个解决方案

#1


1  

Instead of int.parseInt(UserInput), you want Integer.parseInt(UserInput). The Integer class is the corresponding Object class for the primitive type int. This method that you are trying to access, parseInt(String value), is within that Integer class.

而不是int.parseInt(UserInput),您需要Integer.parseInt(UserInput)。 Integer类是基本类型int的对应Object类。您尝试访问的此方法parseInt(String value)位于该Integer类中。

The error you are recieving makes sense, as int doesn't have any methods as it is a primitive type. It is telling you that you are trying to call a method from a primitive type, and that instead, a class is expected to be calling a method.

你收到的错误是有道理的,因为int没有任何方法,因为它是一个原始类型。它告诉您,您正在尝试从基本类型调用方法,而是希望类调用方法。

#1


1  

Instead of int.parseInt(UserInput), you want Integer.parseInt(UserInput). The Integer class is the corresponding Object class for the primitive type int. This method that you are trying to access, parseInt(String value), is within that Integer class.

而不是int.parseInt(UserInput),您需要Integer.parseInt(UserInput)。 Integer类是基本类型int的对应Object类。您尝试访问的此方法parseInt(String value)位于该Integer类中。

The error you are recieving makes sense, as int doesn't have any methods as it is a primitive type. It is telling you that you are trying to call a method from a primitive type, and that instead, a class is expected to be calling a method.

你收到的错误是有道理的,因为int没有任何方法,因为它是一个原始类型。它告诉您,您正在尝试从基本类型调用方法,而是希望类调用方法。