简体   繁体   English

如何同时执行“if”和“else”?

[英]How to execute the “if” and “else” at the same time?

Interesting problem:有趣的问题:

Change if statement and print out "Hello World"更改 if 语句并打印出“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?我的解决方法是在if语句中添加"!System.out.println("Hello")",但是不行,有什么办法吗?

    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:在 C 中:

main()
{   if(printf("Hello"),0)
         printf("Hello");
    else
       printf(" world!\n");
   getch();
}

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

I can offer我可以提供

if (System.out.printf("%s","Hello ") == null) {
    System.out.println("Hello");
} else {
    System.out.println("World");
}

It doesn't work because in Java if expects an expression of type boolean .它不起作用,因为在 Java 中if需要类型为boolean的表达式。 System.out.println has no return type, it is void . System.out.println没有返回类型,它是void That's why it doesn't work.这就是它不起作用的原因。

My 2 cents: None of the solutions, including the orginigal C solution actually execute both the ' if ' and the ' else '.我的 2 美分:没有一个解决方案,包括原始的 C 解决方案实际上同时执行 ' if ' 和 ' else '。 All the solutions presented here execute and explicit printf("Hello") as part of the boolean expression in the condition .此处提供的所有解决方案都执行并显式printf("Hello")作为condition 中布尔表达式的一部分。 In all solutions, that condition is false and the else branch is then executed.在所有解决方案中,该条件为假,然后执行else分支。 But the actual if is not .但实际的if不是

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

Switch on the contents of args and run the program twice with two different parameters.打开args的内容并使用两个不同的参数运行程序两次。

Your program will either print out "Hello" or "World" unless you modify the input to println in addition to fixing your if() construct.您的程序将打印出“Hello”或“World”,除非您修改println的输入并修复if()构造。

Consider the below code to execute both if-else block statements in C.考虑下面的代码在 C 中执行两个 if-else 块语句。

#include <stdio.h>
#include <stdbool.h>
int main()
{

  bool flag = false;
  if(flag)
  {
    printf("if block returns true-> now redirecting to else");
    goto el;
    i:
    printf(" redirected to if");
  }
  else
  {
    printf("if block returns false-> now redirecting to if");
    goto i;
    el:
    printf(" redirected to else");
  }
}

Just change the flag value in the above code.只需更改上面代码中的标志值即可。 For example, if the flag value changed to true then first if block will execute and then else.例如,如果标志值更改为 true,则首先执行 if 块,然后执行 else。

You could use append instead of println to determine if the write was successful:您可以使用append而不是println来确定写入是否成功:

if (System.out.append("Hello World" + System.getProperty("line.separator")) != null)
{
    // some code here
}

This can also be achieved using "printf" with out specifying the format.这也可以使用“printf”而不指定格式来实现。 the code goes as below:代码如下:

if (System.out.printf("Hello") == null) {
    System.out.print("Hello");
} else {
    System.out.println("World");
}

Check this one...检查这个...

class Test {

    static boolean x = true;

    public static void main(String[] args) {
        ifAndElse();

    }

    public static void ifAndElse(){
        if (x) {
            System.out.println("hi");
            x = false;
            ifAndElse();
        } else {
            System.out.println("hello");
        }
    }
}
** Try this one **
class Test
{
    public static void main(String args[])
    {
        boolean one=true;
        for(int i=0;i<2;i++)
        {
        if(one)
        {
            System.out.print("Hello");
            one=false;  
        }   
        else

            System.out.println("World!");

        }       
    }
}`
public class Test {  

       public static void main(String args[]) {  

            if (args==null || new Test() {{Test.main(null);}}.equals(null)) {  
                     System.out.print("Hello ");
            } else {  
                     System.out.print("World!");  
            }  
       }  
}  

this can work, but a little bit complicate for understanding这可以工作,但理解起来有点复杂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM