⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 getting command-line input.htm

📁 点net代码20个
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Getting Command</title>
</head>

<body background="../basic3.gif">

<table border="0" cellspacing="0" width="596">
  <tr>
    <td align="center"><b><font color="#FF0000" size="4">Getting Command-Line Input</font></b></td> 
  </tr> 
  <tr> 
    <td></td> 
  </tr> 
  <tr> 
    <td><font color="#008000">// Namespace Declaration<br> 
      using System;<br> 
      <br> 
      // Program start class<br> 
      class NamedWelcome {<br> 
      <br> 
      &nbsp;&nbsp;&nbsp; // Main begins program execution.<br> 
      &nbsp;&nbsp;&nbsp; public static void Main(string[] args) {<br> 
      <br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Write to console<br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Hello,  
      {0}!&quot;, args[0]);<br> 
      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Welcome  
      to the C# Station Tutorial!&quot;);&nbsp;<br> 
      <br> 
      &nbsp;&nbsp;&nbsp; }<br> 
      }<br> 
      </font></td> 
  </tr> 
  <tr> 
    <td>Remember to add your name to the command-line, i.e. &quot;NamedWelcome  
      Joe&quot;.&nbsp; If you don't, your program will crash.&nbsp; I'll show  
      you in a later lesson how to detect and avoid such error conditions.</td> 
  </tr> 
  <tr> 
    <td>In Listing 1-2, you'll notice an entry in the &quot;Main&quot; method's  
      parameter list.&nbsp; The parameter name is &quot;args&quot;.&nbsp; It's  
      what you use to refer to the parameter later in your program.&nbsp; The  
      &quot;string[]&quot; expression defines the Type of parameter that &quot;args&quot;  
      is.&nbsp; The &quot;string&quot; Type holds characters.&nbsp; These  
      characters&nbsp; could form a single word, or multiple words.&nbsp; The  
      &quot;[]&quot;, square brackets denote an Array, which is like a list.&nbsp;  
      Therefore, the Type of the &quot;args&quot; parameter, is a list of words  
      from the command-line.</td> 
  </tr> 
  <tr> 
    <td>You'll also notice an additional &quot;Console.WriteLine(...)&quot;  
      statement within the &quot;Main&quot; method.&nbsp; The argument list  
      within this statement is different than before.&nbsp; It has a formatted  
      string with a &quot;{0}&quot; parameter embedded in it.&nbsp; The first  
      parameter in a formatted string begins at number 0, the second is 1, and  
      so on.&nbsp;&nbsp; The &quot;{0}&quot; parameter means that the next  
      argument following the end quote will determine what goes in that  
      position.&nbsp; Hold that thought, and now we'll look at the next argument  
      following the end quote.&nbsp;&nbsp;</td> 
  </tr> 
  <tr> 
    <td>This is the &quot;args[0]&quot; argument, which refers to the first  
      string in the &quot;args&quot; array.&nbsp; The first element of an Array  
      is number 0, the second is number 1, and so on.&nbsp; For example, if I  
      wrote &quot;NamedWelcome Joe&quot; on the command-line, the value of  
      &quot;args[0]&quot; would be &quot;Joe&quot;.</td> 
  </tr> 
  <tr> 
    <td>Now we'll get back to the embedded &quot;{0}&quot; parameter in the  
      formatted string.&nbsp; Since &quot;args[0]&quot; is the first argument,  
      after the formatted string, of the &quot;Console.WriteLine()&quot;  
      statement, it's value will be placed into the first embedded parameter of  
      the formatted string.&nbsp; When this command is executed, the value of  
      &quot;args[0]&quot;, which is &quot;Joe&quot; will replace &quot;{0}&quot;  
      in the formatted string.&nbsp; Upon execution of the command-line with  
      &quot;NamedWelcome Joe&quot;, the output will be as follows:</td> 
  </tr> 
</table> 
<dl> 
  <dd>&gt;Hello, Joe! 
  <dd>&gt;Welcome to the C# Station Tutorial!</dd> 
</dl> 
<p>Another way to provide input to a program is via the console.&nbsp; Listing  
1-3 shows how to obtain interactive input from the user.</p> 
<h5>Listing 1-3. Getting Interactive Input:&nbsp; InteractiveWelcome.cs</h5> 
<dl> 
  <dd><font color="#008000">// Namespace Declaration<br> 
    using System;<br> 
    <br> 
    // Program start class<br> 
    class NamedWelcome {<br> 
    <br> 
    &nbsp;&nbsp;&nbsp; // Main begins program execution.<br> 
    &nbsp;&nbsp;&nbsp; public static void Main() {<br> 
    <br> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Write to console/get input<br> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.Write(&quot;What is your  
    name?: &quot;);<br> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.Write(&quot;Hello, {0}!  
    &quot;, Console.ReadLine());<br> 
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Console.WriteLine(&quot;Welcome  
    to the C# Station Tutorial!&quot;);&nbsp;<br> 
    <br> 
    &nbsp;&nbsp;&nbsp; }<br> 
    }<br> 
    </font></dd> 
</dl> 
<p>This time, the &quot;Main&quot; method doesn't have any parameters.&nbsp;  
However, there are now three statements and the first two are different from the  
third.&nbsp; They are &quot;Console.Write(...)&quot; instead of &quot;Console.WriteLine(...)&quot;.&nbsp;  
The difference is that the &quot;Console.Write(...)&quot; statement writes to  
the console and stops on the same line, but the &quot;Console.WriteLine(...)&quot;  
goes to the next line after writing to the console.&nbsp;&nbsp;</p> 
<p>The first statement simply writes &quot;What is your name?:&nbsp; &quot; to  
the console.&nbsp;&nbsp;</p> 
<p>The second statement doesn't write anything until it's arguments are properly  
evaluated.&nbsp; The first argument after the formatted string is &quot;Console.ReadLine()&quot;.&nbsp;  
This causes the program to wait for user input at the console, followed by a  
Return or Enter.&nbsp; The return value from this method replaces the  
&quot;{0}&quot; parameter of the formatted string and is written to the console.&nbsp;&nbsp;</p> 
<p>The last statement writes to the console as described earlier.&nbsp; Upon  
execution of the command-line with &quot;InteractiveWelcome&quot;, the output  
will be as follows:</p> 
<dl> 
  <dd>&gt;What is your Name?&nbsp; &lt;type your name here&gt;<br> 
    &gt;Hello, &lt;your name here&gt;!&nbsp; Welcome to the C# Station Tutorial!</dd> 
</dl> 
<p>By now you know the basic structure of a C# program.&nbsp; You are familiar  
with namespaces and classes.&nbsp; You know the &quot;Main&quot; method is your  
entry point to start a C# program.&nbsp; You've also learned how to capture  
command-line input and perform interactive I/O.</p> 
<p>This is just the beginning, the first of many lessons.&nbsp; I invite you  
back to take Lesson 2:&nbsp; Expressions, Types, and Variables&nbsp; (Now  
complete).&nbsp;</p> 
<p>Your feedback is very important and I appreciate any constructive comments  
you have. Please feel free to contact me for any questions or comments you may  
have about this lesson.</p> 
 
</body> 
 
</html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -