三个数由小到大排序
关键技术
if else语句和三元运算符
代码如下:
1.C
(示例):
#include "stdio.h"
main()
{
int a, b, c, t; /*定义四个基本整型变量a,b,c,t*/
printf("please input a,b,c:\n"); /*双引号内普通字符原样输出并换行*/
scanf("%d%d%d", &a, &b, &c); /*输入任意三个数*/
if (a > b) /*如果a大于b,借助中间变量t实现a,b值互换*/
{
t = a;
a = b;
b = t;
}
if (a > c) /*如果a大于c,借助中间变量t实现a,c值互换*/
{
t = a;
a = c;
c = t;
}
if (b > c) /*如果b大于c,借助中间变量t实现b,c值互换*/
{
t = b;
b = c;
c = t;
}
printf("the order of the number is:\n");
printf("%d,%d,%d", a, b, c); /*输出函数将a,b,c的值顺序输出*/
}
java
(示例):
/**
* @author zwking
* @E-mail zwyc16@163.com
* @create 2021-09-13 15:21
*/
import java.util.Scanner;
/**
* 三个数由小到大排序
*/
public class Demo02 {
public static void main(String[] args) {
//键盘录入
Scanner scanner = new Scanner(System.in);
System.out.print("请输入第一个数a1:");
int a1 = scanner.nextInt();
System.out.print("请输入第二个数a2:");
int a2=scanner.nextInt();
System.out.print("请输入第三个数a3:");
int a3=scanner.nextInt();
// 获取最大值
int max=(a1>a2)? a1:a2;
max=(max>a3)? max:a3;
// 获取最小值
int min=(a1<a2)? a1:a2;
min=(min<a3)? min:a3;
// 获取中间值
int mid=a1+a3+a2-max-min;
System.out.println(min+"<"+mid+"<"+max);
![请添加图片描述](https://img-blog.csdnimg.cn/028030c670194084ad0f3161886076f9.PNG)
}
}