简体   繁体   English

仅使用 if/else 在 Java 中按降序排列的数字

[英]Numbers in descending order in Java using if/else only

How can I create a java program using only if / else to order 3 numbers in descending order.如何仅使用 if / else 以降序排列 3 个数字来创建 Java 程序。 I can not use for processes or array.我不能用于进程或数组。 Numbers are entered by the user.数字由用户输入。 Here's what i have so far.这是我到目前为止所拥有的。 What should i do, is the user enters two integers that are the same?我该怎么办,用户是否输入了两个相同的整数? The code can only display one output.代码只能显示一个输出。

    import java.util.Scanner; 
public class DescendingOrder
{
   public static void main(String [] args)
   {
      //variable dec.
      int a;
      int b;
      int c;
      Scanner kbd = new Scanner(System.in);

      //user prompt
      System.out.println("Please enter three integers");
      a=kbd.nextInt();
      b=kbd.nextInt();
      c=kbd.nextInt();

      //program output
      if (a>=b && b>=c && a>=c)
      {
         System.out.println("a b c");
      }

      if (a>=c && c>=b && a>=b )
      {   
         System.out.println("a c b");
      }   

      if (b>=a && a>=c && b>=c)
      {
         System.out.println("b a c");
      }

      if (b>=c && c>=a && b>=c)
      {      
          System.out.println("b c a");
      }

      if(c>=a && a>=b && c>=b)
      {
          System.out.println("c a b");
      }

      if (c>= b && b>=a && c>=a)
      {
         System.out.println("c b a"); 
      }   


   }
}

A simpler way you can do it is一种更简单的方法是

if (a < b)
{
    int temp = a;
    a = b;
    b = temp;
}

if (b < c)
{
    int temp = b;
    b = c;
    c = temp;
}


if (a < b)
{
    int temp = a;
    a = b;
    b = temp;
}
System.out.println(a + " " + b + " " + c);

If two numbers are the same it doesn't really matter, as 90 45 45 is the same as 90 45 45. (In the case of your code as written, however, you are correct in noticing that it does matter. You could fix this by changing all your if statements except the first one into else-if)如果两个数字相同,则无关紧要,因为 90 45 45 与 90 45 45 相同。(但是,在您编写的代码的情况下,您注意到它确实很重要是正确的。您可以修复通过将除第一个之外的所有 if 语句更改为 else-if 来实现这一点)

This question seems to be a thought exercise so consider this answer an alternative to the other correct answers for your consideration.这个问题似乎是一个思考练习,因此请考虑将此答案作为其他正确答案的替代方案供您考虑。 Here my enterprise-y solution.这是我的企业级解决方案。

Step 0, refactor your code to make it testable and stub out the method that will do the actual work:第 0 步,重构您的代码以使其可测试并存根执行实际工作的方法:

import static java.lang.System.in;
import static java.lang.System.out;

import java.util.Scanner;

public class DescendingOrder {

    public static final void main(final String... args) { // unavoidable use of an array, please don't dock points
        try (final Scanner kbd = new Scanner(in)) { // always close your Closeables
            final int a = kbd.nextInt();
            final int b = kbd.nextInt();
            final int c = kbd.nextInt();
            final DescendingOrder calculator = new DescendingOrder();
            out.println(calculator.order(a, b, c));
        }
    }

    public String order(final int a, final int b, final int c) {
        return null;
    }
}

Step 1, write a unit test:第一步,编写单元测试:

import static java.lang.Integer.MAX_VALUE;
import static java.lang.Integer.MIN_VALUE;
import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

public class DescendingOrderTest {

    private DescendingOrder orderer;

    @Before
    public void setUp() throws Exception {
        orderer = new DescendingOrder();
    }

    @Test
    public final void testOrderABC() {
        final String result = orderer.order(MAX_VALUE, 0, MIN_VALUE); // don't forget the edge cases
        assertEquals(MAX_VALUE + " " + 0 + " " + MIN_VALUE, result);
    }

    @Test
    public final void testOrderACB() {
        final String result = orderer.order(13, 5, 8);
        assertEquals("13 8 5", result);
    }

    @Test
    public final void testOrderBAC() {
        final String result = orderer.order(4, 8, 2);
        assertEquals("8 4 2", result);
    }

    @Test
    public final void testOrderBCA() {
        final String result = orderer.order(-8, -2, -4); // don't forget negative numbers
        assertEquals("-2 -4 -8", result);
    }

    @Test
    public final void testOrderCAB() {
        final String result = orderer.order(1, -5, 5);
        assertEquals("5 1 -5", result);
    }

    @Test
    public final void testOrderCBA() {
        final String result = orderer.order(MAX_VALUE, 0, MIN_VALUE);
        assertEquals(MAX_VALUE + " " + 0 + " " + MIN_VALUE, result);
    }

    @Test
    public final void testAllSame() {
        final String result = orderer.order(53, 53, 53);
        assertEquals("53 53 53", result);
    }
}

Step 2, iteratively implement order until your tests pass:第 2 步,迭代执行顺序,直到您的测试通过:

    public String order(final int a, final int b, final int c) {
        if (a > b && a > c) {
            return a + " " + order(b, c);
        } else if (b > a && b > c) {
            return b + " " + order(a, c);
        }
        return c + " " + order(a, b);
    }

    protected String order(final int x, final int y) {
        if (x > y) {
            return x + " " + y;
        }
        return y + " " + x;
    }

It might not be the most computationally efficient, but I kept the method sizes small so it is clear what the code is meant to accomplish.它可能不是计算效率最高的,但我保持方法的大小很小,因此很清楚代码的目的是完成什么。 I also do not need to scan through six scenarios to see that it is correct.我也不需要扫描六个场景来查看它是否正确。 If I assume that order( int, int ) is correct, then I only need to work through three scenarios to see that order( int, int, int ) is correct.如果我假设order( int, int )是正确的,那么我只需要通过三个场景来查看order( int, int, int )是否正确。

Obviously, this is overkill.显然,这太过分了。 But those constraints would never really exist.但这些限制永远不会真正存在。

I think your code was fine you were just printing wrong.我认为您的代码很好,您只是打印错误。 You have to understand that a,b,c are variable of type int.你必须明白 a,b,c 是 int 类型的变量。 when you use the + operator with two ints it performs an addition.当您将 + 运算符与两个整数一起使用时,它会执行加法运算。 If you use the + operator with an int and with a string it produces a concatenation.如果将 + 运算符与 int 和 string 一起使用,则会产生串联。 The int 'calls' to string turning the int into a string which produces String + String = concatenation. int 'calls' to string 将 int 转换为产生 String + String = 连接的字符串。 Anyways, I think this is what you wanted.无论如何,我认为这就是你想要的。 Let me know if this is what you wanted.让我知道这是否是您想要的。

    import java.util.Scanner; 
public class Stackoverflow
{
   public static void main(String [] args)
   {
      //variable dec.
      int a;
      int b;
      int c;
      Scanner kbd = new Scanner(System.in);

      //user prompt
      System.out.println("Please enter three integers");
      a=kbd.nextInt();
      b=kbd.nextInt();
      c=kbd.nextInt();

      //program output
      if (a>=b && b>=c && a>=c)
      {
         System.out.println(a+" "+b+" "+c);
      }

      if (a>=c && c>=b && a>=b )
      {   
         System.out.println(a+" "+c+" "+b);
      }   

      if (b>=a && a>=c && b>=c)
      {
         System.out.println(b+" "+a+" "+c);
      }

      if (b>=c && c>=a && b>=c)
      {      
          System.out.println(b+" "+c+" "+a);
      }

      if(c>=a && a>=b && c>=b)
      {
          System.out.println(c+" "+a+" "+b);
      }

      if (c>= b && b>=a && c>=a)
      {
         System.out.println(c+" "+b+" "+a); 
      }   


   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM