C#初学者 [英] C# Beginner

查看:72
本文介绍了C#初学者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习编写C#程序,并从msdn C#教程开始.我已经对第一个程序(Hello World)遇到了麻烦:).

I am learning to write C# programs and started off with msdn C# tutorials. I am already stuck :) with a problem with my first program (Hello World).

// Hello3.cs
// arguments: A B C D
using System;

public class Hello3
{
   public static void Main(string[] args)
   {
      Console.WriteLine("Hello, World!");
      Console.WriteLine("You entered the following {0} command line arguments:",
         args.Length );
      for (int i=0; i < args.Length; i++)
      {
         Console.WriteLine("{0}", args[i]);
      }
   }
}



当我执行上面的代码时,我希望打开一个控制台窗口供我键入参数,但是即使在我看到其中的内容之前,它都会执行并且该窗口消失.我不知道该怎么办??????

我插入了"Console.ReadKey()",但窗口仍然停留在执行中,它消失了?



When I execute the above code, I expect a console window to open up for me to type in arguments, but it executes and the window dissapears even before I can see what is there in it. I have no idea what to do??????

I inserted the "Console.ReadKey()", but still the window doesnt stay on execution, it dissapears???

推荐答案

过程执行,显示结果,并清理.最简单的方法是等待按键;
The proces executes, displays the results, and cleans up. The easy way out is to wait for a keypress;
for (int i=0; i < args.Length; i++)
{
   Console.WriteLine("{0}", args[i]);
   Console.ReadKey();
}



--edit;
另外,您也可以在调试器之外运行代码.如果您按下Ctrl-F5,则应启动该应用程序,并要求其在退出之前按任意键.


祝您好运:)



--edit;
As an alternative, you can run your code outside the debugger; if you hit Ctrl-F5, the app should be launched and it would ask to press any key before it exits.


Good luck :)


好吧,没有在打开的命令提示符中输入参数,而是在从命令行或运行"对话框调用exe时传递了参数.

如果要从Visual Studio本身传递命令行参数(通常是为了测试命令行实用程序而完成),请按照以下步骤操作:
1.打开您的解决方案的属性.
2.选择调试"选项卡.
3.在开始选项"中,提供要传递的参数.

Well, arguments are not entered in the Command Prompt that opens up rather they are passed while calling your exe from command line or Run dialog.

If you want to pass command line arguments from Visual Studio itself (usually done for testing command line utilities) then follow the following steps:
1. Open Properties for your solution.
2. Select Debug tab.
3. In Start Options, provide the arguments that you want to pass.

It would work perfectly after that.


只需添加其他人的发言,值得添加提示,以便您知道何时结束:
Just to add to what the others have said, it is worth adding a prompt so that you know when it has finished:
public static void Main(string[] args)
{
   Console.WriteLine("Hello, World!");
   Console.WriteLine("You entered the following {0} command line arguments:",
      args.Length );
   for (int i=0; i < args.Length; i++)
   {
      Console.WriteLine("{0}", args[i]);
   }
   Console.Write("[End: Press any key to exit]");
   Console.ReadKey();
}

最好让您的用户(即使只有您自己)知道您正在等待他做某事!除此之外,对于第一个程序来说完全不是一件容易的事. :-D

It is always a good idea to let your user (even if it is only you) know you are waiting for him to do something! Other than that, not a bad effort for a first program at all. :-D


这篇关于C#初学者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆