利用函数调运,实现数的交换(排大小)

来源:互联网 发布:高考的意义知乎 编辑:程序博客网 时间:2024/06/06 21:02

代码:

import java.util.Scanner;
public class textch {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 Scanner reader=new Scanner(System.in);
 System.out.println("请连续输入三个数,每输入一个,按回车键以确认:");
 int x=reader.nextInt();
 int y=reader.nextInt();
 int z=reader.nextInt();
 ch CH=new ch();
 CH.exchange(x,y,z);
 }

}

public class ch {
 void exchange(int a,int b,int c)
 {
  int count=0;
  int temp=0;
  if(a>b)
  {
   temp=a;
   a=b;
   b=temp;
   count++;
   System.out.println("第"+count+"次交换的结果是"+a+","+b+","+c);
  }
  if(a>c)
  {
   temp=a;
   a=c;
   c=temp;
   count++;
   System.out.println("第"+count+"次交换的结果是"+a+","+b+","+c);
  }
  if(b>c)
  {
   temp=b;
   b=c;
   c=temp;
   count++;
   System.out.println("第"+count+"次交换的结果是"+a+","+b+","+c);
  }
  if(count==0)
   System.out.println("第"+count+"次交换的结果是"+a+","+b+","+c);
  
 }

}


原创粉丝点击