从键盘接收十个整数。 比较大小后输出最大的和最小的怎么实现?
从键盘接收十个整数。 比较大小后输出最大的和最小的怎么实现?
想到下面这个例子里只接收一次, 现在接收十次不是要接收十次了
我想肯定不是这样的。。。
import java.io.*;
public class ReceiveNumber {
/**
* @param args
*/
public static void main(String[] args) {
int i = 0;
String s = null;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
s = br.readLine();
} catch(
IOException e) {}
System.out.println( "You 've input the number: " + s);
System.out.println( "\r ");
int m = s.indexOf( ". ");
String p = s.substring(0,m);
String q = s.substring(m+1,s.length());
System.out.println( "The integer of the number is "+ p);
System.out.println( "\r ");
System.out.println( "The decimal fraction of the number is "+ q);
}
------解决方案--------------------接收完了放在数组i里
然后用 Arrays.sort(i); 就可以了
前提导入java.util.*;
给你个例子运行下看看就明白了...输入建议用Scanner
import java.util.*;
public class Sort {
public static void main(String[] args) {
double[] i = new double[5];
Scanner in = new Scanner(System.in);
for (int j = 0; j < 5; j++) {
i[j]=in.nextDouble();
}
for (int j = 0; j < 5; j++) {
System.out.print(i[j]+ "\t ");
}
System.out.println( " ");
Arrays.sort(i);
for (int j = 0; j < 5; j++) {
System.out.print(i[j]+ "\t ");
}
}
}