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. *
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 In开发者_如何学PythonputStreamReader(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. :)
int temp = 0;
should be double temp = 0;
and
int len = intValue.length[];
should be int len = intValue.length;
and
for (i = 0; i < 10; i++);
should be 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] + ", ");
}
}
}
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
temp = intValue[j];
Also using an IDE like Eclipse/NetBeans/IntelliJ would definitely help!
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.
精彩评论