输入三个整数a、b、c,按由小到大的顺序进行输出
//实现三个数从小到大的排序
public static void Sort()
{
Console.WriteLine("请输入三个整数回车分开:\n");
/*此处的 x y z 分别从控制台获取三个键盘输入的数据
并且此处的 int.Parse作用:将往控制台输入的数据转成int整型数据
*/
int x = int.Parse(Console.ReadLine());
int y = int.Parse(Console.ReadLine());
int z = int.Parse(Console.ReadLine());
int t;
/*此处的三个if和C语言一致*/
if (x > y)
{
t = x;
x = y;
y = t;
}
if (x > z)
{
t = x;
x = z;
z = t;
}
if (y > z)
{
t = y;
y = z;
z = t;
}
//将排好的数据写入控制台
Console.WriteLine("排好的数为:x=" + x + " y=" + y + " z=" + z + "");
//将控制台上接收到的数据屏幕输出
Console.ReadLine();
}
在主函数中调用:
static void Main(string[] args)
{
Sort();
}
package test;
import java.util.Scanner;
public class StringBuilderTest02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
int[] arr = { a, b, c };
int mid = 0;
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (arr[j] < arr[i]) {
mid = arr[j];
arr[j] = arr[i];
arr[i] = mid;
}
}
}
for (int i = 0; i < 3; i++) {
System.out.println(arr[i]);
}
}
}
运行效果 :