编写Java程序_输⼊三个整数x,y,z,请把这三个数由⼩到⼤输出,请写出实现代码。(3种⽅法)
要求说明:
输⼊三个整数x,y,z,请把这三个数由⼩到⼤输出。
实现代码:
第1种⽅法:
import java.util.Scanner;
public class xyzMaxMin{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输⼊3个整数:");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
System.out.println("从⼩到⼤排序后的结果:");
if (x < y && x < z) {
if (y < z) {
System.out.println(x + "<" + y + "<" + z);
} else {
System.out.println(x + "<" + z + "<" + y);
}
} else if (y < x && y < z) {
if (x < z) {
System.out.println(y + "<" + x + "<" + z);
} else {
System.out.println(y + "<" + z + "<" + x);
}
} else {
if (x < y) {
System.out.println(z + "<" + x + "<" + y);
} else {
System.out.println(z + "<" + y + "<" + x);
}
}
}
}
第2种⽅法:
import java.util.Scanner;
public class xyzMaxMin{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输⼊3个整数:");
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
int i = 0;
if(x>y){
i=y;
y=x;
x=i;
}
if(x>z){
i=z;
z=x;
nextint()方法x=i;
}
if(y>z){
i=z;
z=y;
y=i;
}
System.out.println("从⼩到⼤排序后的结果:");    System.out.println(x+"<"+y+"<"+z);
}
}
第3种⽅法:
import java.util.Arrays;
import java.util.Scanner;
public class xyzMaxMin{
public static void main(String[] args) {
int[] num = new int[3];
Scanner sc = new Scanner(System.in);
System.out.println("请输⼊3个整数:");
num[0] = sc.nextInt();
num[1] = sc.nextInt();
num[2] = sc.nextInt();
Arrays.sort(num);
System.out.println("从⼩到⼤排序后的结果:");  for (int i = 0; i < num.length; i++) {
System.out.print(num[i]+"\t");
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。