开发者

combine multiple if statements

How do i make my program only use one if statement and an else statement?

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<开发者_JAVA技巧;number2){
            System.out.println("LESSER THAN");
        }
    }
}


if(number1==number2){

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

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


Here use this:

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

    }
}


A variant of qbert solution, using 'java.lang.ArithmeticException' and without allocating memory:

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


Another solution, in my opinion much better than the 'ArithmeticException' solution I previously submitted:

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

To explain this a little, when 'number1' is gt 'number2', 'i' is negative. The leftmost bit of a number is 1 when it is negative, 0 otherwise. So I get this bit, with 'i & (1 << 31)' shit it right of 31, which gives me -1 for a negative number, 0 otherwise. And then I only have to do the array lookup to get the result.


try this

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

without if using ternary operator


My idea, avoiding ternary operator. It allocates an array, catching the exception if the array size is negative.

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜