这个程序问题出在哪里?

danlella 2004-04-25 12:26:57
import java.io.*;
import java.util.*;
/*输入整型数组,输入需排系的数组,创建对象SortArray
调用其排系函数对其排系

public class TextSort
{
public static void main(String[] arg)throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
{
String input=JoptionPane.showInputDialog
("How many numbers do you want to sort?");
int n=Integer.parseInt(input);
int[]array=new int[n];
System.out.println("please input the number to be sorted one by one,and each one end with ENTER");
for(i=0;i<n;i++)
{

{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
array[i]=Integer.parseInt(s) ;
}
}
Sort SortArray=new sort();
SortArray.QuickSort(array,0,n-1,n);
}
/*类Sort:包含两个函数,通过递归调用和相互调用,实现快排
public class Sort throws IOException
{
public Sort( );
public void QuickSort(int[]L,int first,int last,int number)
{
int n=number;
if(first<last)
{
split=partition(L[],first,last);
for(i=0;i<n;i++)System.out.println(L[i]);
QuickSort(l,first,last,split-1,n);
QuickSort(l,split+1,last,n);
}
return;
}
public int Partition(int L[],int first,int last)
{
left=first;
right=last;
int i=1;
pivot=L[left];
System.out.println
("It's the"+ i+++"time for Partition funtion to be used" );
System.out.println
("the variety value of first and last is :"first","last);
while(left<right)
{
while((right>left)&&(L[right]>=pivot)) right--;
L[left++]=L[right];
while((left<right)&&(L[left]<pivot)) left++;
if(left<last)L[right--]=L[left];
}
System.out.println("the divider's index and value is :"left,L[left]);
L[left]=pivot;
return left;
}
private int pivot;
private int left;
private int right;
}

这个程序编译老出错误,不知道是什么原因
还希望能改进一下程序结构
请多多指教

...全文
45 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sangern 2004-04-27
  • 打赏
  • 举报
回复
这么长
看都看晕了
没时间了
Sikemu 2004-04-27
  • 打赏
  • 举报
回复
不好意思啊!还是有问题啊!!!!!
彻底晕了!
Sikemu 2004-04-27
  • 打赏
  • 举报
回复
import java.io.*;
import java.util.*;
import javax.swing.*; //引入 这个包,JOptionPane在这个包里面
public class TextSort //输入需排系的数组,创建新的对象用于调用其排系函数

{
public static void main(String[] arg)throws IOException
{

String input=JOptionPane.showInputDialog("How many numbers do you want to sort?");
int n=Integer.parseInt(input);
int[]array=new int[n];

System.out.println("please input the number to be sorted one by one,and each one end with ENTER");
for(int i=0;i<n;i++)
{
try
{

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); //reader is already defined in main
String s = reader.readLine();
array[i]=Integer.parseInt(s); // s is already defined in main
}catch(IOException e){}
}
Sort SortArray=new Sort();
SortArray.QuickSort(array,0,n-1,n);

}
}

class Sort //一个java文件中,只允许有一个类是public的
{
int pivot;
int left;
int right;
int temp;
//left=first;
// right=last;
int count=0;//记录Partition函数被调用的次数
// public Sort( );//默认情况下自动生成空构造函数
public void QuickSort(int L[],int first,int last,int number)
{
right=last;
left=first;
int n=number;
if(first<last)
{
int split=Partition(L,first,last);
for(int i=0;i<n;i++) //i未声明,
System.out.println(L[i]);
QuickSort(L,left,split-1,n);
QuickSort(L,split+1,last,n);

}
return;
}
public int Partition(int L[],int first,int last)
{
count++;
pivot=L[left];
System.out.println
("It's the第"+ count +"次 for Partition funtion to be used" );
System.out.println
("the variety value of first and last is :"+first+","+last);
while(left<right)
{
while((right>left)&&(L[right]>=pivot))
{
right--;
}
while((right<left)&&(L[left]<=pivot))
{
left++;
}
if(left<right)
{
temp=L[left];
L[left]=L[right];
L[right]=temp;
}
else
{
temp=pivot;
pivot=L[left];
L[left]=temp;

}
}

System.out.println("the divider's index and value is :"+left+","+L[left]);
L[left]=pivot;
return left;
}

}
如此,就OK了!不过,这个程序有点复杂了!!绕了好大一个弯!
阎罗 2004-04-27
  • 打赏
  • 举报
回复
String input=JoptionPane.showInputDialog("How many numbers do you want to sort?");//怎么能界面赋值给一个字符串呢应该是
String input="How many numbers do you want to sort?";
for(i=0;i<n;i++)//i没有定义 应该是int i = 0;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();//这两句都不需要因为String s 在上面已经定义过了
Sort SortArray=new sort();//第二个sort应该是大写的Sort();

import java.io.*;
import java.util.*;


public class TextSort
{
public static void main(String[] arg)throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
String input = "How many numbers do you want to sort?";
int n=Integer.parseInt(input);
int[] array = new int[n];
System.out.println("please input the number to be sorted one by one,and each one end with ENTER");
for(int i = 0; i<n; i++)
{
array[i]=Integer.parseInt(s) ;
}
Sort SortArray=new Sort();
SortArray.QuickSort(array,0,n-1,n);
}
}
danlella 2004-04-27
  • 打赏
  • 举报
回复
TestSort是用来输入需排序的数组,
创建Sort 对象SortArray,调用它的方法对数组
进行排序
如果没有这个类,Sort类内的算法是无法驱动的
阎罗 2004-04-27
  • 打赏
  • 举报
回复
大哥你这个真是要你命三千啊,头都大了
修改后的
import java.io.*;
import java.util.*;


public class Sort
{
private int pivot;
private int left;
private int right;

public Sort()
{
}

public void QuickSort(int[] L, int first, int last, int number)
{
int n=number;
if(first < last)
{
int split = Partition(L, first, last);
for(int i=0;i<n;i++)
{
System.out.println(L[i]);
}
QuickSort(L,first,last,n);
QuickSort(L,split+1,last,n);
}
return;
}


public int Partition(int L[],int first,int last)
{
left = first;
right = last;
int i = 1;
pivot=L[left];
System.out.println("It's the"+ i++ +"time for Partition funtion to be used");
System.out.println("the variety value of first and last is :" + first + "," +last);
while(left<right)
{
while((right>left)&&(L[right]>=pivot)) right--;
L[left++]=L[right];
while((left<right)&&(L[left]<pivot)) left++;
if(left<last)L[right--]=L[left];
}
System.out.println("the divider's index and value is :" + left + "," + L[left]);
L[left]=pivot;
return left;
}
}
至于TextSort类我就根本就搞不懂你想干什么,看不懂
Sikemu 2004-04-27
  • 打赏
  • 举报
回复
来了!
19830711 2004-04-25
  • 打赏
  • 举报
回复
关注
升烟 2004-04-25
  • 打赏
  • 举报
回复
先看看基础吧,一句话两句话说不清楚 ,java语法也还是要考虑的

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧