JAVA定义一个类IntSort,在IntSort 类中定义2 个重载的方法sort(),用来对不同的整数进行排序并以字符串形式返回排序结果。

  1. 程序功能:定义一个类IntSort,在IntSort 类中定义2 个重载的方法sort(),用来对不同的整数进行排序并以字符串形式返回排序结果。其中第一个方法String sort(int a, int b)用来对两个整数进行排序并返回排序结果,第二个方法String sort (int a, int b, int c) 用来对三个整数进行排序并返回排序结果。最后,定义一个主类KY5_1,在类KY5_1中定义一个main()方法,在该main方法中定义三个整形变量a,b,c,并创建一个IntSort类的对象s,调用对象s的2 个sort()方法,分别对两个变量a和b以及三个变量a,b和c进行排序,并以字符串形式输出排序结果。
  2. 编译KY5_1.java并运行程序
  3. 将源程序写在实验报告中。
package test;
class IntSort
{
    
    
	void Stringsort(int a,int b)
	{
    
    
		int temp;
		if(a<b)
			{
    
    temp=a;
		    a=b;
		    b=temp;
		    System.out.printf("%d %d", a,b);}
		else
			System.out.printf("%d %d", b,a);
			
			
	}
	
	void Stringsort(int a,int b,int c)
	{
    
    
		int t;
		 if(a>b)

		    {
    
    t = a;
		    a = b;
		    b = t;
		    }

		    if(a>c)

		    {
    
    
		        t = a;
		        a = c;
		        c = t;

		    }
		    if(b>c)

		    {
    
    
		        t = b;
		        b = c;
		        c = t;
		    }
		    System.out.printf("%d %d %d", c,b,a);
		  
	}
}

/*执行*/
public class KY5_1{
    
    
public static void main(String[] args) 
{
    
    
	IntSort s = new IntSort();
	int a=1,b=2,c=3;
	s. String sort( a, b);
	s.String sort(a, b,c);
}}

猜你喜欢

转载自blog.csdn.net/qq_43952288/article/details/106926598
今日推荐