java数字比较进阶
来源:互联网 发布:linux nano 全选 编辑:程序博客网 时间:2024/06/05 05:55
<script type="text/javascript">google_ad_client = "pub-8800625213955058";/* 336x280, 创建于 07-11-21 */google_ad_slot = "0989131976";google_ad_width = 336;google_ad_height = 280;//</script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>第一个程序:/** * AWT Sample application * * @author * @version 1.00 05/03/08 */public class Chapter3 { int a,b,c; public void sort(int i,int q,int p) { if(i>q&&i>p) { a=i; if(q>p) { b=q; c=p; } else { b=p; c=q; } } if(q>i&&q>p) { a=q; if(i>p) { b=i; c=p; } else { b=p; c=i; } } if(p>i&&p>q) { a=p; if(i>q) { b=i; c=q; } else { b=q; c=i; } } System.out.print(a "/t"); System.out.print(b "/t"); System.out.print(c "/t"); } public static void main(String[] args) { System.out.println("OutPut from big to small"); Chapter3 cha=new Chapter3(); cha.sort(3,6,9); System.out.println(); }}第二个程序:/** * AWT Sample application * * @author * @version 1.00 05/04/26 */package MyPackage.Stream;import java.io.*;import java.util.*;public class Compare{public static void sort(double arry[],int n)//排序函数{ // for(int j=0;j<=n 1;j ) for(;;) { for(int q=0;q<n-1;q ) if(arry[q]>arry[q 1]) { double t; t=arry[q]; arry[q]=arry[q 1]; arry[q 1]=t; } n--; if(n<0) break; }}public static String In() throws IOException{String str;BufferedReader Input=new BufferedReader(new InputStreamReader(System.in));//数据流进行输入 str=Input.readLine();//读取行。 return str;} public static void main(String[] args) throws IOException { String st; int num; int i=0; double buffer[]; System.out.println("Input the Number to compare:"); st=In(); num=Integer.parseInt(st);//类型转换String->int buffer=new double[num];//动态定义数组存储数据 System.out.println("Input your Numbers:"); while(true)//保存 { st=In(); double f=Double.parseDouble(st);//类型转换String->double buffer[i]=f; i ; if(i==num) break; } sort(buffer,num);//进行数字的排序。 System.out.println("The Number to sort is:"); for(i=0;i<num;i )//排完序列后输出。 { System.out.println(buffer[i]); } }}第三个程序:/** * AWT Sample application * * @author * @version 1.00 05/04/26 */package MyPackage.Stream;import java.io.*;import java.util.*;public class Compare{public static String In() throws IOException{String str;BufferedReader Input=new BufferedReader(new InputStreamReader(System.in));//数据流进行输入 str=Input.readLine();//读取行。 return str;} public static void main(String[] args) throws IOException { String st; int num; int i=0; double buffer[]; System.out.println("Input the Number to compare:"); st=In(); num=Integer.parseInt(st);//类型转换String->int buffer=new double[num];//动态定义数组存储数据 System.out.println("Input your Numbers:"); while(true)//保存 { st=In(); double f=Double.parseDouble(st);//类型转换String->double buffer[i]=f; i ; if(i==num) break; } sort(buffer,num);//进行数字的排序。 Arrays.sort(buffer); System.out.println("The Number to sort is:"); for(i=0;i<num;i )//排完序列后输出。 { System.out.println(buffer[i]); } }}第四个程序:/** *输入有错误,给出提示,退出。 */import java.io.*;import java.util.*;public class Compare{public static String In() throws IOException{String str;BufferedReader Input=new BufferedReader(new InputStreamReader(System.in));//数据流进行输入 str=Input.readLine();//读取行。 return str;}public static void PutIn(double buffer[]) throws IOException{ String st; int i=0; while(true)//保存 { try { st=In(); double f=Double.parseDouble(st);//类型转换String->double buffer[i]=f; i ; if(i==buffer.length) break; } catch(NumberFormatException e) { e.printStackTrace(); System.out.println("程序异常,请按回车退出程序!"); System.exit(0); } } Arrays.sort(buffer);//进行数字的排序? System.out.println("The Number to sort is:"); } public static void PrintOut(double buffer[]) { for(int i=0;i<buffer.length;i )//排完序列后输出。 { System.out.println(buffer[i]); } } public static void main(String[] args) throws IOException { try { String st; int num; for(;;) { System.out.println("Input the Number to compare:"); st=In(); num=Integer.parseInt(st);//类型转换String->int if(num<=0)//判断数据的个数的正确性! { System.out.println("Please input a number which must bigger than zero!"); } else { break; } } double buffer[]=new double[num];//动态定义数组存储数据 System.out.println("Input your Numbers:"); PutIn(buffer); PrintOut(buffer); } catch(NumberFormatException e) { System.out.println("程序异常"); e.printStackTrace(); } }}本来还有一个,不知道放在那里了。不过我想这么多也够了。呵呵。