C# 控制台用户输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11252612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# Console User Input
提问by Brandon
Ok, I want to first start off by saying I'm not a student so this question has nothing to do with homework at all. I'm trying to learn C# because the company that I want to work for uses it. I heard that C# is very similar to java so I'm using my java book that has exercise problems to practice c#. Here is my question, I'm trying to make a simple program that the user enters 3 grades and it stores it in an array and then displays the three grades that were entered. The problem is that its not storing the grades. It does however display some random number like if I put in 34, 44, and 54 it returns 51. Here is my code and thanks everyone:
好的,我想首先说我不是学生,所以这个问题与家庭作业完全无关。我正在尝试学习 C#,因为我想工作的公司使用它。我听说 C# 与 java 非常相似,所以我正在使用我有练习题的 java 书来练习 c#。这是我的问题,我正在尝试制作一个简单的程序,用户输入 3 个成绩并将其存储在一个数组中,然后显示输入的三个成绩。问题是它不存储成绩。然而,它确实显示了一些随机数,就像我输入 34、44 和 54 一样,它返回 51。这是我的代码,谢谢大家:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Practice1
{
class Program
{
static void Main(string[] args)
{
int[] test = new int[4];
int i = 1;
for (i = 1; i <= 3; i++)
{
Console.WriteLine("Please enter test " + i);
test[i] = Console.Read();
Console.ReadLine();
}
for (i = 1; i <=3; i++)
{
Console.WriteLine(test[i]);
Console.ReadLine();
}
}
}
}
回答by Jonathon Reinhart
Console.Read()returns a character. You want to read a stringfrom the console, convert it to an int, and then store that value in your array.
Console.Read()返回一个字符。您想string从控制台读取 a ,将其转换为 an int,然后将该值存储在您的数组中。
回答by agent-j
Your problem is here:
你的问题在这里:
test[i] = Console.Read();
This is putting a character (which is an integer character code) into your test array.
这是将一个字符(它是一个整数字符代码)放入您的测试数组中。
Instead do
而是做
test[i] = int.Parse(Console.ReadLine());
Edit:If you aren't certain that the user will type a parsable integer, maybe they'll type in "six", for example you might consider using a try/catch (if you want to know why it wouldn't parse), or the int.TryParse, which returns true to indicate success and assigns the parsed integer to a variable, field, or array index:
编辑:如果您不确定用户是否会输入可解析的整数,也许他们会输入“六”,例如您可能会考虑使用 try/catch(如果您想知道为什么它不会解析) ,或 int.TryParse,它返回 true 以指示成功并将解析的整数分配给变量、字段或数组索引:
if(int.TryParse(Console.ReadLine(), out test[1])
Console.WriteLine("Successfully parsed integer");
else
Console.WriteLine("Please enter an integer.");
回答by Eric J.
Console.Read() returns the ASCII value of the key entered. For example if you type in "A", you get the value 65 which is the ASCII code for "A".
Console.Read() 返回输入的键的 ASCII 值。例如,如果您输入“A”,则会得到值 65,它是“A”的 ASCII 代码。
You will need to parse your string to an integer:
您需要将字符串解析为整数:
for (i = 0; i < 4; i++)
{
Console.WriteLine("Please enter test " + i);
string input = Console.ReadLine();
int value;
bool success = int.TryParse(input, out value);
if (success)
{
test[i] = value
}
else
{
// Show an error message that the user must enter an integer.
}
Console.ReadLine();
}
Also note that arrays are indexed starting with 0 in C#, not with 1 as your code assumes.
另请注意,数组在 C# 中从 0 开始索引,而不是像您的代码假定的那样从 1 开始。
Alternatively you can still use Console.Read(), which returns the integer representation of the character entered, confirm that the character is in fact a number, and convert from the ASCII code to the appropriate number.
或者,您仍然可以使用Console.Read(),它返回输入字符的整数表示,确认该字符实际上是一个数字,然后从 ASCII 代码转换为适当的数字。
回答by NominSim
From the docsConsole.Read()"Reads the next character from the standard input stream."
来自文档Console.Read()“从标准输入流中读取下一个字符”。
You want the next Integer, so something like
你想要下一个整数,所以像
bool cont = false;
int val = 0;
do
{
cont = int.TryParse(Console.ReadLine(), out val);
if(!cont){Console.WriteLine( "please enter a real number you fool" );}
} while (!cont);
Should work.
应该管用。
回答by eyossi
int[] test = new int[3];
for (int i = 0; i < 3; i++)
{
Console.WriteLine("Please enter test " + i + 1);
test[i] = Int.Parse(Console.ReadLine());
}
for (int i = 0; i < 3; i++)
{
Console.WriteLine(test[i]);
Console.ReadLine();
}
As you can see, arrays starts from index 0, so there is no need to define int[4] (one more int than required), and you need to loop from index 0 to length-1
如您所见,数组从索引 0 开始,因此不需要定义 int[4](比所需的多一个 int),并且需要从索引 0 循环到 length-1
回答by Adam Gritt
The problem is that you are reading in the character. As such the "51" you are seeing is the decimal (base 10) ASCII value for the number 3. What you need to do is the following:
问题是您正在阅读角色。因此,您看到的“51”是数字 3 的十进制(基数为 10)ASCII 值。您需要做的是以下内容:
string result = Console.ReadLine();
int grade = 0;
int.TryParse(result, out grade)
test[i] = grade;
回答by user6142764
Here is the code:
这是代码:
int[] test = new int[3];
int[] test = new int[3];
for (int e = 0; e < 3; e++)
{
Console.WriteLine("Please enter test ");
test[e] = int.Parse(Console.ReadLine());
}
Console.WriteLine("000000000000000000000000000\n");
for (int e = 0; e < 3; e++)
{
Console.WriteLine(test[e]);
//Console.ReadLine();
}

