为什么我得到'.class'预期错误?简单数组脚本

时间:2011-09-23 21:29:51

标签: java arrays

我的程序正在创建一个数组,并允许用户输入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]);

        }

        }
    }
}

感谢您的任何意见。 :)

3 个答案:

答案 0 :(得分:0)

int temp = 0;应为double temp = 0;

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

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

Sample


修改

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] + ", ");

        }

    }
}

答案 1 :(得分:0)

int len = intValue.length[];

你在长度之后不需要[]并且你也尝试将int temp分配给double数组中的值

temp = intValue[j];

同样使用像Eclipse / NetBeans / IntelliJ这样的IDE肯定会有所帮助!

答案 2 :(得分:0)

int len = intValue.length[];

应改为:

int len = intValue.length;

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

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

更改为:

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

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