欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

实现对三个整数进行排序,输出时按照从小到大的顺序输出。

程序员文章站 2022-03-03 16:09:18
...

实现对三个整数进行排序,输出时按照从小到大的顺序输出。

import java.util.Scanner;
class Test7 
{
	public static void main(String[] args){
	Scanner sca = new Scanner(System.in);
	System.out.println("第一个整数:");
	int num1 = sca.nextInt();
	System.out.println("第二个整数:");
	int num2 = sca.nextInt();
	System.out.println("第三个整数:");
	int num3 = sca.nextInt();             //输入三个整数
	
	if(num1 >= num2){                     //num1 >= num2的所有情况
		if(num2 >= num3){
			System.out.println(num3 + "," + num2 + "," + num1);
		}else if(num3 >= num1){
			System.out.println(num2 + "," + num1 + "," + num3);
		}else{
			System.out.println(num2 + "," + num3 + "," + num1);
		}
	}else{                                 //num1 >= num2的所有情况
		if(num2 <= num3){
			System.out.println(num1 + "," + num2 + "," + num3);
		}else if(num3 <= num1){
			System.out.println(num3 + "," + num1 + "," + num2);
		}else{
			System.out.println(num1 + "," + num3 + "," + num2);
		}
	}
  }
}

更详细的博客

相关标签: java