English 中文(简体)
如何同时执行"如果"和"else"?
原标题:How to execute the "if" and "else" at the same time?
  • 时间:2012-05-25 17:34:53
  •  标签:
  • java

有趣的问题:

如果更改语句并打印出“ 你好世界”

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

C:

main()
{   if(printf("Hello"),0)
         printf("Hello");
    else
       printf(" world!
");
   getch();
}
最佳回答

塔卡亚:

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");
    }
}
问题回答

我可以出价

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

它不起作用,因为在 Java if 中,希望表达型号 boolean System.out.println 没有返回类型,它是 evue 。 这就是为什么它不起作用。

My 2 cents: None of the solutions, including the orginigal C solution actually execute both the if and the else . All the solutions presented here execute and explicit printf("Hello") as part of the boolean expression in the condition. In all solutions, that condition is false and the else branch is then executed. But the actual if is not.

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

切换 args 的内容,用两个不同的参数运行两次程序。

您的程序将打印出“ 你好” 或“ 世界” 除非您将输入修改为 println , 并修补您的 if () 构造 。

考虑以下代码来执行 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.

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

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

也可以使用“打印”和指定格式的“打印”。 代码如下:

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

看看这个...

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

这能起作用, 但有点复杂 理解





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签