import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class GetMaxNum {
private Number[] source;
public Number[] makeArray(String str, String sep) {
String[] arr;
if (str != null && !str.equals("")) {
arr = str.split(sep);
} else {
arr = new String[0];
}
this.source = new Number[arr.length];
for (int i = 0; i < arr.length; i++) {
source[i] = Double.parseDouble(arr[i]);
}
return source;
}
/*
* 冒泡排序方法:递增顺序
*/
public Number[] doSort_asc() {
for (int i = source.length - 1; i > 1; i--) {
for (int j = 0; j < i; j++) {
if (source[j].doubleValue() > source[j + 1].doubleValue()) {
Number tmp = source[j];
source[j] = source[j + 1];
source[j + 1] = tmp;
}
}
}
return source;
}
/*
* 冒泡排序方法:递减顺序
*/
public Number[] doSort_desc() {
for (int i = source.length - 1; i > 1; i--) {
for (int j = 0; j < i; j++) {
if (source[j].doubleValue() < source[j + 1].doubleValue()) {
Number tmp = source[j];
source[j] = source[j + 1];
source[j + 1] = tmp;
}
}
}
return source;
}
/*
* 所有数据显示
*/
public void display() {
for (int i = 0; i < source.length; i++) {
System.out.print(source[i] + " ");
}
System.out.print("\n");
}
public void display(Number[] source) {
for (int i = 0; i < source.length; i++) {
System.out.print(source[i] + " ");
}
System.out.print("\n");
}
/*
* 显示前两个
*/
public void display2() {
for (int i = 0; i < source.length; i++) {
if (i < 2) {
System.out.print(source[i] + " ");
}
}
System.out.print("\n");
}
public void display2(Number[] source) {
for (int i = 0; i < source.length; i++) {
if (i < 2) {
System.out.print(source[i] + " ");
}
}
System.out.print("\n");
}
public static void main(String[] args) throws IOException {
System.out.println("请输入一个数值类型的字符串(空格分隔):");
BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
String str = buffer.readLine();
GetMaxNum gmn = new GetMaxNum();
Number[] num = gmn.makeArray(str, " ");
Number[] num2 = gmn.doSort_desc();
System.out.println("你的要求结果:");
gmn.display2(num2);
gmn = null;
num = null;
num2 = null;
str = null;
buffer = null;
}
}
//注释:以上实现原理,从键盘输入任意的数值型字符串,空格隔开,回车结束;立即显示结果:你输入值的最大值和此大值,当然还可以显示排序(递增或者递减)后的值,自己测试一下结果吧!
本文如未解决您的问题请添加抖音号:51dongshi(抖音搜索懂视),直接咨询即可。