各位大吓帮我看看错哪里了?能编译可执行时总是错误?import java.util.*;
import java.io.*;
public class insertsort {
public static int[] Data=new int[5];
public static void main(String[] args) {
int i;
int Index;
System.out.println("please input the values you want to sort(Exit for 0):");
Index=0;
//InputStreamReader is=new InputStreamReader(System.in);
//BufferedReader br=new BufferedReader(is);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
do
{
//System.out.print("Data"+Index+":");
try
{System.out.print("Data"+Index+":");
//String myline=br.readLine();
//st=new StringTokenizer(myline);
//Data[Index]=Integer.parseInt(st.nextToken());
System.out.flush();
Data[Index]=Integer.parseInt(br.readLine());
}
catch(IOException ioe)
{
System.out.print("IO error:"+ioe);
}
Index++;
}while(Data[Index-1]!=0);
System.out.print("Before Insert Sorting:");
for(i=0;i<Index-1;i++)
System.out.print(" "+Data[i]+" ");
System.out.println();
InsertSort(Index-1);
System.out.print("After insert sorting:");
for(i=0;i<Index-1;i++)
System.out.print(" "+Data[i]+" ");
System.out.println();
}
public static void InsertSort(int Index)
{
int i,j,k;
int InsertNode=0;
for(i=1;i<Index;i++);
{
InsertNode=Data[i];
//j=i-1;
for(j=i-1;j>=0&&InsertNode<Data[j];j--)
{
Data[j+1]=Data[j];
//j--;
}
Data[j+1]=InsertNode;
System.out.println("Current sorting result:");
for(k=0;k<Index;k++)
{
System.out.print(" "+Data[k]+" ");
System.out.println();
}
}
}}
import java.io.*;
public class insertsort {
public static int[] Data=new int[5];
public static void main(String[] args) {
int i;
int Index;
System.out.println("please input the values you want to sort(Exit for 0):");
Index=0;
//InputStreamReader is=new InputStreamReader(System.in);
//BufferedReader br=new BufferedReader(is);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
do
{
//System.out.print("Data"+Index+":");
try
{System.out.print("Data"+Index+":");
//String myline=br.readLine();
//st=new StringTokenizer(myline);
//Data[Index]=Integer.parseInt(st.nextToken());
System.out.flush();
Data[Index]=Integer.parseInt(br.readLine());
}
catch(IOException ioe)
{
System.out.print("IO error:"+ioe);
}
Index++;
}while(Data[Index-1]!=0);
System.out.print("Before Insert Sorting:");
for(i=0;i<Index-1;i++)
System.out.print(" "+Data[i]+" ");
System.out.println();
InsertSort(Index-1);
System.out.print("After insert sorting:");
for(i=0;i<Index-1;i++)
System.out.print(" "+Data[i]+" ");
System.out.println();
}
public static void InsertSort(int Index)
{
int i,j,k;
int InsertNode=0;
for(i=1;i<Index;i++);
{
InsertNode=Data[i];
//j=i-1;
for(j=i-1;j>=0&&InsertNode<Data[j];j--)
{
Data[j+1]=Data[j];
//j--;
}
Data[j+1]=InsertNode;
System.out.println("Current sorting result:");
for(k=0;k<Index;k++)
{
System.out.print(" "+Data[k]+" ");
System.out.println();
}
}
}}
public static void InsertSort(int Index)
{
int i,j,k;
int InsertNode=0;
for(i=1;i <Index;i++); //这里多了一个;号,for循环只执行空语句,下面的代码块只执行一次
{
InsertNode=Data[i];
//j=i-1;
for(j=i-1;j>=0&&InsertNode <Data[j];j--)
{
Data[j+1]=Data[j];
//j--;
}
Data[j+1]=InsertNode;
System.out.println("Current sorting result:");
for(k=0;k <Index;k++)
{
System.out.print(" "+Data[k]+" ");
System.out.println();
}
} 另外一个就是:
do {
// System.out.print("Data"+Index+":");
try {
System.out.print("Data" + Index + ":");
// String myline=br.readLine();
// st=new StringTokenizer(myline);
// Data[Index]=Integer.parseInt(st.nextToken());
System.out.flush();
Data[Index] = Integer.parseInt(br.readLine()); } catch (IOException ioe) {
System.out.print("IO error:" + ioe);
}
Index++;
} while (Data[Index - 1] != 0);
这样读会有问题,lz没有控制可以输入的数的个数,如果输入超过5个数,就会造成ArrayIndexOutOfBound的错误;而且用0来判断结束,如果0是要判断的数呢?另外读入来就直接转为整型Integer.parseInt(br.readLine()),如果别人输入错误怎么办呢?
所在要在循环中加一句do
{
//System.out.print("Data"+Index+":");
try
{System.out.print("Data"+Index+":");
//String myline=br.readLine();
//st=new StringTokenizer(myline);
//Data[Index]=Integer.parseInt(st.nextToken());
System.out.flush();
Data[Index]=Integer.parseInt(br.readLine()); }
catch(IOException ioe)
{
System.out.print("IO error:"+ioe);
}
Index++;
if(Index == 5) break;
}while(Data[Index-1]!=0 );