java 为什么我会收到“.class”预期错误?简单数组脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7535073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:22:50  来源:igfitidea点击:

Why am I getting a '.class' expected error? Simple Array script

javaarrays

提问by user951376

My program is creating an array and allowing the user to input 10 double precision numbers. Then the program will sort them in order from lowest to highest. I have the following but receive .class expected error upon compiling. Any ideas on why this is happening? Note * I have not been able to compile this yet so I don't even know if this will work. *

我的程序正在创建一个数组并允许用户输入 10 个双精度数字。然后程序会按照从低到高的顺序对它们进行排序。我有以下内容,但在编译时收到 .class 预期错误。关于为什么会发生这种情况的任何想法?注意 * 我还不能编译它,所以我什至不知道这是否可行。*

import java.io.*;

public class ArrayDemo
{
    public static void main(String[] args) throws IOException
    {
        int i = 0;
        int j = 0;
        int temp = 0;
        double[] intValue = new double[10];
        String[] numbers = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"};
        int len = intValue.length[];

        BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));

        for (i = 0; i < len; ++i)
            System.out.println("Enter the " + numbers[i] + " number");
        intValue[i] = Double.valueOf(dataIn.readLine());

        {

        for (j = 0; j < (len - 1) -i; j++)
            if (intValue[j] > intValue[j+1]) 
            {
                temp = intValue[j];
                intValue[j] = intValue[j+1];
                intValue[j+1] = temp;
            }

        for (i = 0; i < 10; i++);
        {
            System.out.println("Array after sorting in ascending order");
            System.out.println();
            System.out.println(intValue[i]);

        }

        }
    }
}

Thank you for any input. :)

感谢您提供任何意见。:)

采纳答案by Joe

int temp = 0;should be double temp = 0;

int temp = 0;应该 double temp = 0;

and

int len = intValue.length[];should be int len = intValue.length;

int len = intValue.length[];应该 int len = intValue.length;

and

for (i = 0; i < 10; i++);should be for (i = 0; i < 10; i++)

for (i = 0; i < 10; i++);应该 for (i = 0; i < 10; i++)

Sample

样本



EDIT

编辑

import java.io.*;

public class Main
{
    public static void main(String[] args) throws IOException
    {
        int i = 0;
        int j = 0;
        int k = 0;
        double temp = 0;
        double[] intValue = new double[10];
        String[] numbers = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"};
        int len = intValue.length;

        BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));

        for (i = 0; i < len; ++i) {
            System.out.println("Enter the " + numbers[i] + " number");
            intValue[i] = Double.valueOf(dataIn.readLine());
        }


        for (j = 0; j < len; j++)
        {
            for(k = 0; k < len; k++) {
                if (intValue[j] > intValue[k]) 
                {
                    temp = intValue[j];
                    intValue[j] = intValue[k];
                    intValue[k] = temp;
                }
            }
        }

        System.out.println("Array after sorting in ascending order");
        for (i = 0; i < 10; i++)
        {
            System.out.print(intValue[i] + ", ");

        }

    }
}

回答by Long Ho

int len = intValue.length[];

You don't need [] after length and you also tried to assign int temp to a value in a double array

长度后不需要 [] 并且您还尝试将 int temp 分配给双数组中的值

temp = intValue[j];

Also using an IDE like Eclipse/NetBeans/IntelliJ would definitely help!

使用 Eclipse/NetBeans/IntelliJ 等 IDE 肯定会有所帮助!

回答by Brandon E Taylor

int len = intValue.length[];

should instead be:

应该是:

int len = intValue.length;


Also, some of your bracketing appears to be incorrect. I believe, for example, that you want the following snippet:

此外,您的某些包围似乎不正确。例如,我相信您需要以下代码段:

for (i = 0; i < len; ++i)  
    System.out.println("Enter the " + numbers[i] + " number");
intValue[i] = Double.valueOf(dataIn.readLine());

Changed to:

变成:

for (i = 0; i < len; ++i)
{
   System.out.println("Enter the " + numbers[i] + " number");
   intValue[i] = Double.valueOf(dataIn.readLine());
}


You have a number of other logical errors in your code as well. Let me know, after you work with the code for a while based on the current answers, if you have any specific questions and I will help you further.

您的代码中还有许多其他逻辑错误。让我知道,在您根据当前答案使用代码一段时间后,如果您有任何具体问题,我会进一步帮助您。