三个数比较大小!新手问题!
在控制台输入3个数比较大小,并从小到大输出!高手帮帮忙啊!我初学!!
------解决方案--------------------public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print( "请输入需要几个数进行排序: ");
int totalNum = sc.nextInt();
int[] num = new int[totalNum];
for (int i = 0; i < num.length; i++) {
System.out.printf( "请输入第 %d 个数: ",i+1);
num[i] = sc.nextInt();
}
System.out.println( "排序结果如下: ");
output(sort(num));
}
public static int[] sort(int[] var) {
for (int i = 0; i < var.length - 1; i++) {
for (int j = 1; j < var.length; j++) {
if (var[i] > var[j]) {
int temp = var[i];
var[i] = var[j];
var[j] = temp;
}
}
}
return var;
}
public static void output(int[] out) {
for (int i : out) {
System.out.println(i);
}
}
------解决方案--------------------import java.util.*;
public class InputSort
{
public static void main(String[]args)
{
int []result = new int[3];
for(int i =0;i <result.length;i++)
result[i] = Integer.parseInt(args[i]);
Arrays.sort(result);
for(int j = 0;j <result.length;j++)
System.out.println(result[j]);
}
}
------解决方案--------------------import java.io.*;
public class TestMyString
{
public static void main(String []args)
{
int a,b,c;
int temp;
try{
DataInputStream data=new DataInputStream(System.in);
System.out.println( "请输入第一个数: ");
a=Integer.parseInt(data.readLine());
System.out.println( "请输入第二个数: ");
b=Integer.parseInt(data.readLine());
System.out.println( "请输入第三个数: ");
c=Integer.parseInt(data.readLine());
if(a> b)
{temp=a;a=b;b=temp;}
if(a> c)
{temp=a;a=c;c=temp;}
if(b> c)
{temp=b;b=c;c=temp;}
System.out.println( "由大到小顺序是: "+a+ " "+b+ " "+c);
}catch(
IOException e)
{e.printStackTrace();
}
}
}
------解决方案--------------------input: a, b, c;
sum = a + b + c;
v_max = Math.max(Math.max(a, b), c);
v_min = Math.min(Math.min(a, b), c);
v_mid = sum - v_max - v_min;
System.out.println(v_min + "\t " + v_mid + "\t " + v_max);