按递增顺序显示三个非负整数

时间:2011-02-07 06:07:44

标签: java if-statement

提示用户键入三个非负整数。以递增的顺序显示整数。

import java.util.Scanner;

{
    int number1,number2,number3;
         Scanner keyboard = new Scanner (System.in);
         number1=keyboard.nextInt();
         number2=keyboard.nextInt();
         number3=keyboard.nextInt();

        if(number1<number2&&number1<number3&&number2<number3)
        {
            System.out.println(number1);
            System.out.println(number2);
            System.out.println(number3);
        }

        else if(number1>number2&&number1>number3&&number2>number3)
        {
            System.out.println(number3);
            System.out.println(number2);
            System.out.println(number1);
        }

        else if(number1<number2&&number1<number3&&number2>number3)
        {
            System.out.println(number1);
            System.out.println(number3);
            System.out.println(number2);
        }

        else if(number2>number1&&number2>number3&&number1>number3)
        {
            System.out.println(number3);
            System.out.println(number1);
            System.out.println(number2);
        }

        else if (number3>number1&&number3>2&&number1>number2)
        {
            System.out.println(number2);
            System.out.println(number1);
            System.out.println(number3);
        }

输出:

2 1 3

1 2 3

P.S。我不被允许使用while语句,swing和for。我是java的新手,所以我能想出来。有更快的方法吗?谢谢。

5 个答案:

答案 0 :(得分:2)

这是冒泡排序的实现。它并不比您的实现好多少,但可以让您一窥将来如何使用循环。会发生的事情是,在前两个区块之后,最大值将位于第三个区域。然后下一个块修复剩下的两个数字。您可以将if子句移动到一个单独的函数中,该函数接受您要比较和切换的两个数字作为参数。

public static void main(String[] args) {


 int number1,number2,number3;
 Scanner keyboard = new Scanner (System.in);
 number1=keyboard.nextInt();
 number2=keyboard.nextInt();
 number3=keyboard.nextInt();

 int temp;

 if (number2 < number1){
     temp = number2;
     number2 = number1;
     number1 = temp;
 }

 if (number3 < number2){
     temp = number3;
     number3 = number2;
     number2 = temp;
 }

 if (number2 < number1){
     temp = number2;
     number2 = number1;
     number1 = temp;
 }
  System.out.println(number1);
  System.out.println(number2);
  System.out.println(number3);

}

答案 1 :(得分:0)

对于三个数字,你的方式足够快但如果下一个分配要求你在10个整数的列表上执行相同的任务,会发生什么?还是100?或1,000,000?

当您继续学习编程时,您将了解到您选择解决问题的策略(算法)的性能可能会根据输入的大小而有很大差异。你在尝试这个问题时选择的策略是列举三个数字按大小相关的所有方式 - 因为只有六个,所以不是太糟糕 - 但是对于十个数字,有数百万种方法!更好的算法选择是将它们存储在一个结构中,在这个结构中,它们的大小会逐个增加(通过对它们进行排序),因为大多数排序算法对于非常大的输入集仍然能够很好地运行。

答案 2 :(得分:0)

速度不是问题。正确是。如果number1和number3相等会怎么样?也就是说,这是一个更加紧凑的逻辑,应该是相当有效的。

if (number2 < number1)
{
    temp_number = number1;
    number1 = number2;
    number2 = temp_number;
}

if (number3 < number2)
{
    temp_number = number2;
    number2 = number3;
    number3 = temp_number;

    if (number2 < number1)
    {
        temp_number = number1;
        number1 = number2;
        number2 = temp_number;
    }
}

System.out.println(number1);
System.out.println(number2);
System.out.println(number3);

答案 3 :(得分:0)

由于它是家庭作业,我认为嵌套与它的其他将是一个更清洁的解决方案。它也可以正确地使用相等的值,负值,并且不会交换变量的值。 : - )

    int number1, number2, number3;
    Scanner keyboard = new Scanner(System.in);

    number1 = keyboard.nextInt();
    number2 = keyboard.nextInt();
    number3 = keyboard.nextInt();

    if (number1 < number2) {
        if (number2 < number3) {
            System.out.println("123 " + number1 + number2 + number3);
        } else {
            if (number1 < number3)
                System.out.println("132 " + number1 + number3 + number2);
            else
                System.out.println("231 " + number3 + number1 + number2);
        }
    } else {
        if (number3 < number2) {
            System.out.println("321 " + number3 + number2 + number1);
        } else {
            if (number3 < number1)
                System.out.println("312 " + number2 + number3 + number1);
            else
                System.out.println("213 " + number2 + number1 + number3);
        }
    }

答案 4 :(得分:0)

boolean n2 ;   
if(num1>num2) {
    if(num1>num2 && num1>num3){
        if(num2>num3){
            System.out.println(num1+">"+num2+">"+num3);
            n2=true;
        }
        else {
            System.out.println(num1+">"+num3+">"+num2);
        }
    }
    else if(n2){
        if(num1<num3){
            System.out.println(num2+">"+num3+">"+num1);
        }
    }
}
else {
    System.out.println(num2+">"+num1+">"+num3);
}