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