组合多个if语句

时间:2011-07-20 12:12:17

标签: java

如何使我的程序仅使用一个if语句和一个 else语句

import java.io.*;

public class TwoNum {
    public static void main(String[] args){
        String first="";
        String second="";

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        try{
            System.out.print("Input the first number: ");
            first = in.readLine();

            System.out.print("Input the second number: ");
            second = in.readLine();

        } catch(IOException e){
            System.out.println("Error!");
        }

        int number1=Integer.parseInt(first);
        int number2=Integer.parseInt(second);

        if(number1==number2){
            System.out.println("EQUIVALENT");
        }
        if(number1>number2){
            System.out.println("GREATER THAN");
        }
        if(number1<number2){
            System.out.println("LESSER THAN");
        }
    }
}

7 个答案:

答案 0 :(得分:3)

if(number1==number2){

  System.out.println("EQUIVALENT");
 }
 else if(number1>number2){

 System.out.println("GREATER THAN");
  }
 else{
 System.out.println("LESSER THAN");
}

答案 1 :(得分:2)

这里使用:

import java.io.*;

public class TwoNum {

public static void main(String[] args){

    String first="";
    String second="";

    BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

    try{
        System.out.print("Input the first number: ");
        first = in.readLine();

        System.out.print("Input the second number: ");
        second = in.readLine();

    } catch(IOException e){
        System.out.println("Error!");
    }

    int number1=Integer.parseInt(first);
    int number2=Integer.parseInt(second);

    String result = null;

    if( number1 == number2 )
        result = "EQUIVALENT";
    else
        result = ( number1 > number2 ) ? "GREATER THAN" : "LESS THAN";

    System.out.println( result );

    }
}

答案 2 :(得分:1)

qbert解决方案的一种变体,使用'java.lang.ArithmeticException'而不分配内存:

try {
  1 / (number2-number1);

  if(number1 > number2){
    System.out.println("GREATER THAN");
  } else {
    System.out.println("LESSER THAN");
  }      
}
catch (ArithmeticException e) {
  System.out.println("EQUIVALENT");
}

答案 3 :(得分:1)

另一种解决方案,在我看来比我之前提交的'ArithmeticException'解决方案要好得多:

String res;
int i = number2 - number1;
if (i == 0) {
    res = "EQUIVALENT";
} else {
    String RES[] = { "GREATER THAN", "LESSER THAN" };
    int j = (i & (1 << 31)) >> 31;
    res = RES[j+1];
}
System.out.println(res);

要解释一下,当'number1''number2'时,'i'为负数。数字的最左边位是负数,否则为0。所以我得到了这一点,'i & (1 << 31)'将它右移31,这给了我一个负数,0否则。然后我只需要进行数组查找就可以得到结果。

答案 4 :(得分:0)

试试这个

String msg="";
msg = ((number1==number2) ? "number1 and number2 is equal" : ((number1>number2) ? "number1 is greater than number2" : "number2 is greater than number1"));
System.out.println(msg);

如果不使用三元运算符

答案 5 :(得分:0)

我的想法,避免三元运算符。它分配一个数组,如果数组大小为负则捕获异常。

import java.io.*;

public class TwoNum {
    public static void main(String[] args){
        String first="";
        String second="";

        BufferedReader in=new BufferedReader(new InputStreamReader(System.in));

        try{
            System.out.print("Input the first number: ");
            first = in.readLine();

            System.out.print("Input the second number: ");
            second = in.readLine();

        } catch(IOException e){
            System.out.println("Error!");
        }

        int number1=Integer.parseInt(first);
        int number2=Integer.parseInt(second);

        try {
            int c[] = new int[number2-number1];
            if(number1==number2){
                System.out.println("EQUIVALENT");
            } else {
                System.out.println("LESSER THAN");
            }
        }
        catch (Exception e) {
            System.out.println("GREATER THAN");
        }
    }
}

答案 6 :(得分:0)

String result = (number1==number2) ? "EQUIVALENT" : ((number1 > number2) ? "GREATER THAN" : "LESS THAN");