我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*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);
}
}
导入论坛查看(40)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing