2011-09-23 62 views
1

我的程序正在创建一个数组并允许用户输入10个双精度数字。然后程序会按从低到高的顺序对它们进行排序。我有以下但编译时收到.class预期的错误。任何想法为什么发生这种情况?注*我还没有能够编译这个,所以我甚至不知道这是否会工作。 *为什么我会得到'.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]); 

     } 

     } 
    } 
} 

谢谢您的任何意见。 :)

回答

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

     } 

    } 
} 
+0

谢谢!现在它吐出“输入第一个数字”,“输入第二个数字”等等,而不提示用户输入它在 中输入的数字。System.out.println(“Enter the”+ numbers [i ] +“数字”); intValue [i] = Double.valueOf(dataIn.readLine());我相信的区域 – user951376

+0

dataIn.readLine()调用在for循环之外进行。您需要将它与前面的println()调用一起放在括号内。 –

+0

因此,我将循环内部的行移开,现在它会提示我输入第一个数字,然后继续询问第一个数字,并返回ArrayIndexOutOfBoundsException错误。 – user951376

0
int len = intValue.length[]; 

你不需要[]长度后,你还试图INT温度在双阵列

temp = intValue[j]; 

而且分配给使用值像Eclipse/NetBeans/IntelliJ这样的IDE肯定会有所帮助!

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

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

相关问题