Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute the "if" and "else" at the same time?

Tags:

java

Interesting problem:

Change if statement and print out "Hello World"

    public static void main(String[] args) {
        if(){
            System.out.println("Hello");
        }else{
            System.out.println("World");
        }
    }

My solution is to add "!System.out.println("Hello")" in the if statement,But it doesn't work, any ideas?

    public static void main(String[] args) {
        if(!System.out.println("Hello")){
            System.out.println("Hello");
        }else{
            System.out.println("World");
        }
    }

UPDATE: I think this works:

    public static void main(String args[]) {    
        if(System.out.append("Hello ")==null){
            System.out.print("Hello ");
        }else{
            System.out.println("World");
        } 
    }

In C:

main()
{   if(printf("Hello"),0)
         printf("Hello");
    else
       printf(" world!\n");
   getch();
}
like image 368
John Powel Avatar asked Nov 28 '22 00:11

John Powel


1 Answers

Tadaaaa:

public static void main(String args[]) {    
    if(!new Object() {
        public boolean foo() {
            System.out.print("Hello ");
            return true;
        }
    }.foo()){
        System.out.println("Hello");
    }else{
        System.out.println("World");
    }
}
like image 74
Tudor Avatar answered Jan 01 '23 11:01

Tudor