有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java中仅使用if/else的降序数字

如何仅使用if/else以降序排列3个数字来创建java程序。我不能用于进程或数组。数字由用户输入。 这是我到目前为止所拥有的。我应该怎么做,用户输入两个相同的整数?代码只能显示一个输出

    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"); 
      }   


   }
}

共 (3) 个答案

  1. # 1 楼答案

    我觉得你的代码很好你只是打印错了。你必须明白a,b,c是int类型的变量。当你把+运算符和两个int一起使用时,它会执行加法。如果将+运算符与int和字符串一起使用,则会产生串联。int“调用”string,将int转换为字符串,生成string+string=concatenation。 不管怎样,我想这就是你想要的。如果这是你想要的,请告诉我

        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); 
          }   
    
    
       }
    }
    
  2. # 2 楼答案

    更简单的方法是

    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);
    

    如果两个数字是一样的,这其实并不重要,因为9045和9045是一样的。(但是,对于编写的代码,您注意到这一点是正确的。您可以通过将除第一条if语句之外的所有if语句更改为else if来解决这一问题)

  3. # 3 楼答案

    这个问题似乎是一个思维练习,所以考虑一下这个答案,另一个正确的答案供你考虑。这是我的enterprise-y解决方案

    第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;
        }
    }
    

    第一步,编写单元测试:

    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);
        }
    }
    

    第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;
        }
    

    这可能不是计算效率最高的方法,但我将方法的大小保持在较小的范围内,这样就可以清楚地知道代码要完成什么。我也不需要浏览六个场景来确定它是否正确。如果我假设order( int, int )是正确的,那么我只需要通过三个场景来确定order( int, int, int )是正确的

    显然,这太过分了。但这些约束永远不会真正存在