Is it possible to find out which number is bigger without using an if statement?

三世轮回 提交于 2021-02-08 05:44:34

问题


I'm doing some small program for a beginners programming course and in my program I have 2 variables which hold numbers. Anyway I need to find out which number is bigger and print the appropriate message according to it, for example I have:

int x = 5;
int y = 10;

I need to print:

"it is true that y is bigger than x";

Now the thing is that I know I can use a simple if statement but I'm not allowed to use it, now it makes me wonder, is it even possible? If so, how can I do that? How can I check which number is bigger WITHOUT doing something like:

if (x > y) 
    answer = true;

...

Thanks in advance.


回答1:


Well you can do:

boolean answer = x > y;

The expression x > y is just an expression of type boolean. While boolean expressions are often used for conditions in if statements, loops etc, they don't have to be - simple assignment works fine too.

It sounds like you want the reverse though:

boolean answer = y > x;

Then you can use the value of answer to build the string to display...




回答2:


Use the ternary operator:

System.out.println(x > y ? "It is true that x is greater than y" : "");



回答3:


ternary operator "?:"

String output = (x > y)? "x is greater than y":"y is greater than x"; 



回答4:


The ternary conditional operator that others mentioned will work. Assuming you are looking for creative ways to do this rather than practical ones, here's another method:

int x = 5;
int y = 10;
while(y > x){
  System.out.println("It is true that y is bigger than x.");
  return;
}
System.out.println("It is false that y is bigger than x.");

The while is just acting as a fancy if, because the return means the otherwise infinite loop will only execute at most once.

Here's another example that instead relies upon short-circuit boolean evaluation:

public static void main(String...args){
  int x = 5;
  int y = 10;
  boolean answer = (y > x);
  boolean testTrue = answer && printTrue();
  boolean testFalse = testTrue || printFalse();
}

private static boolean printFalse() {
  System.out.println("It is false that y is bigger than x.");
  return true;
}

private static boolean printTrue() {
  System.out.println("It is true that y is bigger than x.");
  return true;
}

Of course you shouldn't do this in real production code, but it can be fun to think of unorthodox ways to code something and it can be helpful for exploring the language.




回答5:


Your question is tagged as Java but you do not specify Java in your question. In Java there are multiple ways to get the same result that involve testing the boolean expression x > y somehow, such as the ternary operator. I would consider these equivalent to an explicit if statement.

Other possibilities:

  • Compute the square root of x - y. This will raise an exception if y is bigger. Catch the exception in the caller and report that y is the larger quantity. If there is no exception, report that x is the larger.
  • In LISP, Ruby or another language that supports the symbol type, form a list ((symbol x, x), (symbol y, y)) and sort the list. Then report the second symbol as the variable with the larger value.
  • If using assembly, BASIC, PL/1, etc. you can use an arithmetic expression to choose the target of a GOTO statement. Depending on whether x or y is larger, execution will resume at a different part of the code. Or use the list-sorting trick in the previous bullet to select the GOTO label.
  • In general, the expression ((x - y) / abs(x - y) + 1) / 2 will produce 1 if x is larger and 0 if y is larger. This result could be used to choose data, a function, etc. out of a list of two alternatives, producing conditional behavior without an if statement.



回答6:


You could use recursion (but I would not recommend it)

public int compare ( int a , int b )
{
    switch ( a )
    {
          case Integer.MIN_VALUE :
              switch ( b )
              {
                 case Integer.MIN_VALUE :
                    return 0 ;
                 default :
                    return -1 ;
              }
           default :
              switch ( b )
              {
                  case INteger.Min_VALUE :
                      return 1 ;
                  default :
                      return compare ( a-1 , b-1 ) ;
               }

    }
}



回答7:


(a+b)/2 + Abs(a-b)/2 is the bigger number.




回答8:


I know in some languages you can use short-circuit evaluation to construct the answer.

The expression (A && B) always evaluates to B if A is true. If A is false then B is never evaluated. Similarly (A || B) evaluates to B if A is false. If A is true B is never evaluated.

Though I'm not 100% sure of Java, the expression you want is:

String output = ((x > y) && "it is true that X is greater than Y") 
  || (((x < y) && "it is true that X is less than Y")
  || "it is true that X is equal to Y");


来源:https://stackoverflow.com/questions/8099288/is-it-possible-to-find-out-which-number-is-bigger-without-using-an-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!