在java中,编写程序定义一个类,该类具有排序算法,从小到大排序a,b,c,
时间: 2023-11-07 14:04:43 浏览: 77
好的,以下是一个简单的Java程序,定义了一个类并实现了从小到大排序a、b、c三个数的算法:
```java
public class Sort {
public static void main(String[] args) {
int a = 3, b = 1, c = 2;
sort(a, b, c);
System.out.println("a = " + a + ", b = " + b + ", c = " + c);
}
public static void sort(int a, int b, int c) {
if (a > b) {
int temp = a;
a = b;
b = temp;
}
if (a > c) {
int temp = a;
a = c;
c = temp;
}
if (b > c) {
int temp = b;
b = c;
c = temp;
}
}
}
```
在上面的程序中,我们定义了一个名为`Sort`的类,并在`main`方法中定义了三个整型变量a、b、c,分别赋值为3、1、2。然后调用了一个名为`sort`的静态方法,该方法接收三个整型参数,将这三个数从小到大排序,并将排序后的值赋回原来的变量。最后输出这三个变量的值,可以看到它们已经按照从小到大的顺序排列了。
这里的排序算法是比较简单的,使用了三次if语句来实现,但是对于只有三个数的情况,这种算法已经足够有效了。
阅读全文
相关推荐


















