统计信息

  • 访问数:14546
  • 博客数:143
  • 建立时间:2008-01-05
  • 更新时间:2008-10-15
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

P2Q8

2008-01-25 22:59:07

天气: 晴朗 心情: 高兴

/*P2Q8
 * Name:Wang Chaoran
Description:8 (Sorting three integers)
Write a program that sorts three integers. The integers are entered from standard input and stored in variables num1, num2, and num3, respectively.
The program sorts the numbers so that num1 > num2 > num3. 
*/
import java.util.Scanner;
public class P2Q8
{
  public static void main(String[] args)
  {
    //Create a Scanner
    Scanner scan = new Scanner(System.in);
    //Read in the numbers
    System.out.println("Please input your First integer:");
    int num1 = scan.nextInt();
    System.out.println("Please input your Second integer:");
    int num2 = scan.nextInt();
   System.out.println("Please input your Third integer:");
    int num3 = scan.nextInt();
    String origin = num1+ " "+num2+" " +num3;
    //Ensure the first number is the largest
    if (num1 < num2)
    {
     num1 = num1 + num2;
     num2 = num1 - num2;
     num1 = num1 - num2;
                                           
    }
    if(num1 < num3)
    {
    num1 = num1 + num3;
    num3 = num1 - num3;
    num1 = num1 - num3;
    }  
    //Ensure the second number is the second largest
    if(num2<num3)
    {
      num3 = num3 + num2;
     num2 = num3 - num2;
     num3 = num3 - num2;
    }
    //Print the answer
    System.out.println("Original order is "+origin+"\nSorted order is "+
                       num1+" "+ num2+" "+ num3);
  }
}

加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd