首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

【C#】考你一个输出“Hello World”程序,该怎么处理

2012-12-24 
【C#】考你一个输出“Hello World”程序前天同学问我这样一个面试题:完成如下代码,使其输出“Hello World!”if()

【C#】考你一个输出“Hello World”程序
前天同学问我这样一个面试题:完成如下代码,使其输出“Hello World!”

if() printf("Hello");
else printf(" World!");
我很快想到了答案。然后同学告诉我这个题不止一个答案。

如果换成C#,也可以这样问:完成如下代码,使其输出“Hello World!”
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            if (/* 补充这里 */)
                Console.Write("Hello");
            else
                Console.Write(" World!");
        }
    }
}
用C#语言,至少我已经想到了三种不同原理的答案。

欢迎大家想出更多的答案。

我会在若干天以后公布我所想到的答案。
[解决办法]
using System; 
public class HelloWorld 

public HelloWorld() 

Console.WriteLine("HELLO WORLD"); 

public static void Main() 

HelloWorld hw = new HelloWorld(); 




using System; 
public class HelloWorld 

public void helloWorld() 

Console.WriteLine("HELLO WORLD"); 


public static void Main() 

HelloWorld hw = new HelloWorld(); 
hw.HelloWorld(); 


using System; 
interface IHelloWorld 

void writeHelloWorld(); 

public class HelloWorld : IHelloWorld 

public void writeHelloWorld() 

Console.WriteLine("Hello World"); 

public static void Main() 

HelloWorld hw = new HelloWorld(); 
hw.writeHelloWorld(); 




[解决办法]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            if (Convet.ToBool(Console.Write("Hello")))
                Console.Write("Hello");
            else
                Console.Write(" World!");
        }
    }
}

[解决办法]
引用:
C# code

using System;


using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
     ……


学习
[解决办法]
洗耳恭听
[解决办法]
2楼答案不行的,首先改为这样吧:Convert.ToBoolean (Console.Write("Hello")) 。然后还是
错误 参数“1”: 无法从“void”转换为“object”,继续关注
[解决办法]
 学习一下
[解决办法]
new Func<bool>(() => { Console.Write("Hello"); return false; }).Invoke()

没看懂!!!!
[解决办法]
那是。呵呵。有scanf么。
[解决办法]
引用:
new Func<bool>(() => { Console.Write("Hello"); return false; }).Invoke()

没看懂!!!!


引用:
new Func<bool>(() => { Console.Write("Hello"); return false; }).Invoke()

没看懂!!!!


匿名方法,和下面的一样的..

public bool Function()
{
    Console.Write("Hello");
    return false;
}

if(Function()){ ..
[解决办法]
Hello应该要在if里面打印,想一下先
[解决办法]
还真想不出,还有什么样的办法呢,思路与上面完全不一样的,期待!
[解决办法]
我来一个,LINQ

        static void Main(string[] args)
        {
            if ((args = new string[1]{"Hello"}).Length > 0 &&


                args.Any(s=>{Console.Write(s);return false;}))
            {
                Console.Write("Hello");
            }
            else
            {
                Console.Write("World");
            }
        }


[解决办法]
呵呵 ~~~
[解决办法]
还真不会。。。。。。
[解决办法]
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            if (a())
                Console.Write("Hello");
            else
                Console.Write(" World!");
            Console.ReadLine();

        }
        static bool a()
        {
            Console.Write("Hello");
            return false;
        }
    }
}
[解决办法]
        if (Console.Write("Hello") is object)
                Console.Write("Hello");
            else
                Console.Write(" World!");

[解决办法]
想不出来......

[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 0 ? true : ((Func<bool>)delegate { Main(new string[] { "a" }); return false; }).Invoke())


                Console.Write("Hello");
            else
                Console.Write(" World!");
        }
    }
}


[解决办法]
真想不出,还有什么样的办法呢,思路与上面完全不一样的,期待
[解决办法]
综合20#,22#

 static void Main(string[] args)
        {
            if (args.Length == 0 ? (Main(args = new string[]{string.Empty}) is object) : true)
            {
                Console.Write("Hello");
            }
            else
            {
                Console.Write("World");
            }
        }

[解决办法]

//发散一下。。。
 class Program
    {
        class BrotherLi
        {
            public static bool IsGirl
            {
                get
                {
                    Console.Write("Hello");
                    return false;
                }
                set { Console.Write(" World"); }
            }
        }

        static void Main()
        {
            BrotherLi.IsGirl = !BrotherLi.IsGirl;

            Console.ReadKey();
        }
    }

[解决办法]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)


        {
            if (true)Console.Write("Hello World!");else if (false)
                Console.Write("Hello");
            else
                Console.Write(" World!");
        }
    }
}


[解决办法]
【C#】考你一个输出“Hello World”程序,该怎么处理
[解决办法]
多是牛人,学习了
[解决办法]
代码注入【C#】考你一个输出“Hello World”程序,该怎么处理
 这样算不算作弊..
[解决办法]
   
搞了很久才出结果,很难

static void Main(string[] args)
        {
            if (Convert.ToBoolean(Type.GetType("System.Console").GetMethod("Write", new Type[] { System.Type.GetType("System.String") }).Invoke(null, new string[] { "Hello" })))


                Console.Write("Hello");
            else
                Console.Write(" World!");
        }


[解决办法]
这弯太急了......转不过来.......
[解决办法]
如果可以定义变量我有个更妙的方法
....
[解决办法]
up up
[解决办法]
有点意思,呵呵,学习了
[解决办法]
多是牛人,学习了
[解决办法]
如果是C的话
int main(){
    if(!fork()
------解决方案--------------------


(!wait()))   //子进程打印hello,父进程打印world
          printf("hello ");
    else
          printf("world! ");
          

}
[解决办法]
大家真厉害啊!!
[解决办法]
有意思,关注
[解决办法]
参考42楼:

        static int Main(string[] args)
        {
            if ( args.Length==0?Main(new string[]{""})!=1:true )
                Console.Write("Hello");
            else
                Console.Write(" World!");

            return 1;
        }

[解决办法]
27楼的哥子 太牛B了,人才,顶!!
[解决办法]

if (!printf("Hello"))
     printf("Hello");
else
     printf(" World!");

热点排行