4

有趣的问题:

更改 if 语句并打印出“Hello World”

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

我的解决方案是在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");
        }
    }

更新:我认为这有效:

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

在 C 中:

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

12 回答 12

22

太棒了:

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");
    }
}
于 2012-05-25T17:39:44.747 回答
5

我可以提供

if (System.out.printf("%s","Hello ") == null) {
    System.out.println("Hello");
} else {
    System.out.println("World");
}
于 2012-05-25T17:41:12.113 回答
4

它不起作用,因为在 Java 中if需要一个 type 的表达式booleanSystem.out.println没有返回类型,它是void. 这就是为什么它不起作用。

于 2012-05-25T17:36:56.447 回答
4

我的 2 美分:包括原始 C 解决方案在内的所有解决方案都没有实际执行“ if”和“ else”。此处介绍的所有解决方案都作为条件printf("Hello")中的布尔表达式的一部分执行和显式执行 。在所有解决方案中,该条件为假,然后执行分支。但实际并非如此elseif

于 2012-05-25T19:18:51.427 回答
2
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");
        }
    }
}
于 2012-05-25T17:42:05.190 回答
1

打开内容args并使用两个不同的参数运行程序两次。

您的程序将打印出“Hello”或“World”,除非您修改输入println以修复您的if()构造。

于 2012-05-25T17:41:17.563 回答
1

考虑以下代码以在 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");
  }
}

只需更改上述代码中的标志值即可。例如,如果标志值更改为 true,则首先执行 if 块,然后执行 else。

于 2021-01-22T09:05:37.547 回答
0

您可以使用append而不是println确定写入是否成功:

if (System.out.append("Hello World" + System.getProperty("line.separator")) != null)
{
    // some code here
}
于 2012-05-25T17:41:48.727 回答
0

这也可以在不指定格式的情况下使用“printf”来实现。代码如下:

if (System.out.printf("Hello") == null) {
    System.out.print("Hello");
} else {
    System.out.println("World");
}
于 2014-09-24T11:10:38.307 回答
0

检查这个...

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");
        }
    }
}
于 2016-05-18T06:39:52.717 回答
0
** 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!");

        }       
    }
}`
于 2016-08-09T09:11:04.157 回答
0
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!");  
            }  
       }  
}  

这可以工作,但理解起来有点复杂

于 2016-10-27T14:45:04.663 回答