主题:求助一个简单java应用程序!
cncw
[专家分:0] 发布于 2007-11-14 10:50:00
编写一个java应用程序,要求为输入任意多个 double数,并按从小到大的顺序排列。
回复列表 (共5个回复)
沙发
happyboy2007 [专家分:3900] 发布于 2007-11-14 11:58:00
import java.io.*;
import java.util.ArrayList;
class Sort
{
public static void main(String[]args)throws Exception
{
ArrayList<String> list = new ArrayList<String>();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while(!(str=br.readLine()).equals("end"))
{
try
{
Double.parseDouble(str);
}
catch(Exception ex)
{
System.out.println ("输入的数据不正确。请重新输入。");
continue;
}
list.add(str);
}
br.close();
for(int i=0;i<list.size()-1;i++)
{
for(int j=i+1;j<list.size();j++)
{
double n1 = Double.parseDouble(list.get(i));
double n2 = Double.parseDouble(list.get(j));
if(n1>n2)
{
double tmp = n1;
list.set(i,list.get(j));
list.set(j,tmp+"");
}
}
}
for(int i=0;i<list.size();i++)
{
System.out.println (list.get(i));
}
}
}
板凳
justforfun626 [专家分:18460] 发布于 2007-11-14 12:44:00
为了中国软件工业的未来,有爱心的朋友请不要帮忙代做作业,或者至少是收费服务!
如果您不需要钱,或者您不愿收费用于自己,请把收取的钱用于支援山区贫困学生。。。
谢谢大家!!!
3 楼
haoboy0817 [专家分:880] 发布于 2007-11-14 12:48:00
import javax.swing.JOptionPane;
class SortedTest
{
static int n=100;
double []a=new double[1024];
static void sort(double a[])
{
for(int i=0;i<a.length-1;i++)
for(int j=i;j<a.length;j++)
{
if(a[i]>a[j])
{
double temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
System.out.println("排序后的结果为:");
for(int k=0;k<a.length;k++)
{
System.out.print(a[k]+" ");
}
}
public static void main(String[] args)
{
int i=0;
String s=JOptionPane.showInputDialog("请输入您要排序的数字个数:");
n=Integer.parseInt(s);
double []b=new double[n];
while(i<n)
{
String str=JOptionPane.showInputDialog("请依次输入要排序的数字:");
b[i]=Double.parseDouble(str);
i++;
}
SortedTest.sort(b);
}
}
4 楼
justforfun626 [专家分:18460] 发布于 2007-11-14 19:30:00
To haoboy0817
Do you know Java Arrays class has sort function?
[b]You are doing more harm to LZ than good, he/she should be able to do his/her simple homework.
Because of you, he/she will be lazier and lazier.[/b]
Please stop, you can do the excercises by yourself.
Thanks!
5 楼
haoboy0817 [专家分:880] 发布于 2007-11-15 13:25:00
hello!
of course! I know ArrayList!
I understand you ,I aggree with you,too!!
Thank you!!
我来回复