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

【C#】考你一个输出“Hello World”程序解决方案

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

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

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

如果换成C#,也可以这样问:完成如下代码,使其输出“Hello World!”
C# code
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(); 




[解决办法]
C# code
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”,继续关注
[解决办法]
C# code
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HelloWorld{    class Program    {        static void Main(string[] args)        {            if (new Func<bool>(() => { Console.Write("Hello"); return false; }).Invoke())                Console.Write("Hello");            else                Console.Write(" World!");        }    }}
------解决方案--------------------


c++的话,有逗号表达式就比较容易了。c#。还真想不出还有什么写法。
[解决办法]
学习一下
[解决办法]
new Func<bool>(() => { Console.Write("Hello"); return false; }).Invoke()

没看懂!!!!
[解决办法]
那是。呵呵。有scanf么。
[解决办法]

探讨

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

没看懂!!!!

[解决办法]
Hello应该要在if里面打印,想一下先
[解决办法]
还真想不出,还有什么样的办法呢,思路与上面完全不一样的,期待!
[解决办法]
我来一个,LINQ
C# code
        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;
}
}
}
[解决办法]
C# code
        if (Console.Write("Hello") is object)                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)        {            if (args.Length != 0 ? true : ((Func<bool>)delegate { Main(new string[] { "a" }); return false; }).Invoke())                Console.Write("Hello");            else                Console.Write(" World!");        }    }}
[解决办法]
真想不出,还有什么样的办法呢,思路与上面完全不一样的,期待
[解决办法]
综合20#,22#

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


[解决办法]

C# code
//发散一下。。。 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();        }    }
[解决办法]
C# code
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# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

[解决办法]
学习了

[解决办法]
探讨

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

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

[解决办法]
看看了,在看看
[解决办法]
真不错
[解决办法]
C/C++ code
  if (!printf("Hello"))     printf("Hello");  else      printf(" World!");
[解决办法]
探讨

C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

[解决办法]
C# code
   搞了很久才出结果,很难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!");        }
[解决办法]
这弯太急了......转不过来.......
[解决办法]
如果可以定义变量我有个更妙的方法
C# code
    public static int i = 1;    static int Main(string[] args)    {        if (!(i-->0&&Main(null) is object))            Console.Write("Hello");        else            Console.Write(" World!");        return 1;    } 


[解决办法]
这个。。。不会。看了恍然。
[解决办法]
刚刚发现不要变量也可以

C# code
    static void Main(string[] args)    {        if ((args == null||Main(null) is object))            Console.Write("Hello");        else            Console.Write(" World!");    }
[解决办法]
上面这方法受 《盗梦空间》启发

"我们要进入到下一层梦境"
我们也要进入下一层main函数
[解决办法]
探讨

刚刚发现不要变量也可以C# code
static void Main(string[] args)
{
if ((args == null||Main(null) is object))
Console.Write("Hello");
else
Console.Write(" World!");
……

[解决办法]
想不出来,看来还差得远啊
[解决办法]
....
[解决办法]
up up
[解决办法]
有点意思,呵呵,学习了
[解决办法]
多是牛人,学习了
[解决办法]
如果是C的话
int main(){
if(!fork()||(!wait())) //子进程打印hello,父进程打印world
printf("hello ");
else
printf("world! ");


}
[解决办法]
大家真厉害啊!!
[解决办法]
有意思,关注
[解决办法]
参考42楼:
C# code
        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了,人才,顶!!
[解决办法]
[code=C/C++][/code]
if (!printf("Hello"))
printf("Hello");
else
printf(" World!");
[解决办法]
up,自己都没想到
[解决办法]
学习。。。。你人不是一般的多啊!
[解决办法]
C# code
if (args.Length != 0 ? true : ((Func<bool>)delegate { Main(new string[] { "a" }); return false; }).Invoke())  Console.Write("Hello");else  Console.Write("World");
[解决办法]
学习永无止境啊!那么多方法。顶起!
[解决办法]
给个比较邪恶的,只是个思路,可以改为匿名函数

C# code
using System;namespace HelloWorld{    class Program    {        static void Main(string[] args)        {            if (Exception())                Console.Write("Hello");            else                Console.Write(" World!");        }        static bool Exception()        {            Console.WriteLine("Hello World");            throw (new Exception());        }    }} 


[解决办法]
好像只有这么一种思路:在if条件中注入 打印Hello, 然后再令条件为false 使程序继续打印World.

还有其他的吗?

27.....-_-!!!

[解决办法]
真不错!可惜1#被占的太没水准了
[解决办法]
真不错 .
[解决办法]
学习下!
[解决办法]

[解决办法]
哎,其实方法还真挺多的哦。楼上写了那么多。
[解决办法]
每天坚持顶帖
[解决办法]
不会C#
C的很简洁
#include<stdio.h>

void main()
{
if((printf("hello"))!=0)
printf(" world!");
else
printf(" world!");
}
[解决办法]
找。。。抽。。。。
[解决办法]

[解决办法]
一种是 LINQ
一种是 递归 MAIN

都是透过另一个“函数”达到目的。

[解决办法]

[解决办法]

探讨
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

[解决办法]
确实很有创意
[解决办法]
探讨
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

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

[解决办法]
learning......
[解决办法]
探讨
Hello应该要在if里面打印,想一下先

[解决办法]
这个挺有意思,顶一个!
[解决办法]
不错。。。
[解决办法]
受教了
[解决办法]
学习了
[解决办法]
看看,学习了,以前还真没遇到过
[解决办法]
还真不会。。。。。。
[解决办法]
嗯 基本没试过在条件判断中写这些。。。。
[解决办法]

[解决办法]

------解决方案--------------------


看了27楼的回复,脑海中如度天劫...雷声滚滚..连绵不绝...
[解决办法]

[解决办法]
新人来学习一下
[解决办法]
C#和.net
[解决办法]
。。。。。。。。。。。。。
[解决办法]
这么 多人回答啊,凑凑热闹
[解决办法]
我是新手,学学了
[解决办法]

C# code
if (args.Length != 0 ? true : ((Func<bool>)delegate { Main(new string[] { "a" }); return false; }).Invoke())  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)
……

[解决办法]
学习啦~
[解决办法]
呵呵。。好帖。。学习啦

热点排行