C# 控制台。读取不返回我的int32

C# 控制台。读取不返回我的int32,c#,C#,我不明白为什么我的整数不能正确显示,Console.Read()方法说它返回一个整数,为什么WriteLine不能正确显示它 int dimension; dimension = Console.Read(); Console.WriteLine(""+ dimension); Console.Read()只返回键入内容的第一个字符。您应该使用控制台.ReadLine(): 示例: int suppliedInt; Console.WriteLine("Please enter a numb

我不明白为什么我的整数不能正确显示,Console.Read()方法说它返回一个整数,为什么WriteLine不能正确显示它

int dimension;
dimension = Console.Read();
Console.WriteLine(""+ dimension);
Console.Read()
只返回键入内容的第一个字符。您应该使用
控制台.ReadLine()

示例:

int suppliedInt;

Console.WriteLine("Please enter a number greater than zero");
Int32.TryParse(Console.ReadLine(), out suppliedInt);

if (suppliedInt > 0) {
    Console.WriteLine("You entered: " + suppliedInt);
}
else {
    Console.WriteLine("You entered an invalid number. Press any key to exit");
}

Console.ReadLine();
其他资源:

int suppliedInt;

Console.WriteLine("Please enter a number greater than zero");
Int32.TryParse(Console.ReadLine(), out suppliedInt);

if (suppliedInt > 0) {
    Console.WriteLine("You entered: " + suppliedInt);
}
else {
    Console.WriteLine("You entered an invalid number. Press any key to exit");
}

Console.ReadLine();
MSDN-

MSDN-

Console.Read()
仅返回键入内容的第一个字符。您应该使用
控制台.ReadLine()

示例:

int suppliedInt;

Console.WriteLine("Please enter a number greater than zero");
Int32.TryParse(Console.ReadLine(), out suppliedInt);

if (suppliedInt > 0) {
    Console.WriteLine("You entered: " + suppliedInt);
}
else {
    Console.WriteLine("You entered an invalid number. Press any key to exit");
}

Console.ReadLine();
其他资源:

int suppliedInt;

Console.WriteLine("Please enter a number greater than zero");
Int32.TryParse(Console.ReadLine(), out suppliedInt);

if (suppliedInt > 0) {
    Console.WriteLine("You entered: " + suppliedInt);
}
else {
    Console.WriteLine("You entered an invalid number. Press any key to exit");
}

Console.ReadLine();
MSDN-


MSDN-

Console.Read方法只返回包裹在
int
中的单个字符,因此仅当您读取的数字只有一位数长时才适用,否则您将始终只获得第一位数

由于
Read
的返回值实际上是一个字符,因此不能将其直接用作整数,需要将其从一个字符解析为一个整数

但是假设您想要一个大于一位的数字,那么您确实需要使用
控制台.ReadLine
,并使用
int.TryParse
将输入转换为整数。如果
int.TryParse
返回
false
,您可以警告用户他提供的输入无效,并再次请求维度

示例代码:

整数维

bool isValidDimension;
do
{
    Console.Write("Dimension: ");

    string input = Console.ReadLine();

    isValidDimension = int.TryParse(input, out dimension);

    if (!isValidDimension)
    {
        Console.WriteLine("Invalid dimension... please try again.");
        Console.WriteLine();
    }
} while (!isValidDimension);

Console.Read
方法只返回一个包裹在
int
中的单个字符,因此仅当您正在读取一个只有一位数的数字时才适用,否则您将始终只获取第一个数字

由于
Read
的返回值实际上是一个字符,因此不能将其直接用作整数,需要将其从一个字符解析为一个整数

但是假设您想要一个大于一位的数字,那么您确实需要使用
控制台.ReadLine
,并使用
int.TryParse
将输入转换为整数。如果
int.TryParse
返回
false
,您可以警告用户他提供的输入无效,并再次请求维度

示例代码:

整数维

bool isValidDimension;
do
{
    Console.Write("Dimension: ");

    string input = Console.ReadLine();

    isValidDimension = int.TryParse(input, out dimension);

    if (!isValidDimension)
    {
        Console.WriteLine("Invalid dimension... please try again.");
        Console.WriteLine();
    }
} while (!isValidDimension);
:

返回值

键入:System.Int32输入流中的下一个字符,或 如果当前没有更多的字符,则为负1(-1) 阅读

:

返回值

键入:System.Int32输入流中的下一个字符,或 如果当前没有更多的字符,则为负1(-1) 阅读


你应该这样做

static void Main() 
{
    int Number;
    string strNumber;

    strNumber = Console.ReadLine();
    Number = int.Parse(strNumber);
    Console.WriteLine("" + dimension);
}

你应该这样做

static void Main() 
{
    int Number;
    string strNumber;

    strNumber = Console.ReadLine();
    Number = int.Parse(strNumber);
    Console.WriteLine("" + dimension);
}

您的程序正在返回,但您没有看到,请查看下面的代码块:

如果输出窗口没有停留,您将无法看到输出

int dimension;
dimension = Console.Read();
Console.WriteLine("" + dimension);
Console.ReadLine();

您的程序正在返回,但您没有看到,请查看下面的代码块:

如果输出窗口没有停留,您将无法看到输出

int dimension;
dimension = Console.Read();
Console.WriteLine("" + dimension);
Console.ReadLine();
Console.Read()返回输入中第一个符号的ASCII码。你能行

int dimension;
dimension = Console.Read();
Console.WriteLine(""+ (char)dimension);
您将在输入中看到右第一个符号,如下所示

(char)dimension
将通过ASCII码为您提供符号。

控制台。Read()返回输入中第一个符号的ASCII码。你能行

int dimension;
dimension = Console.Read();
Console.WriteLine(""+ (char)dimension);
int a = 0;
if(Int32.TryParse(Console.ReadLine(), out a))
{
    // Do your calculations with 'a' 
}
else
{
    // Some warnings
}
您将在输入中看到右第一个符号,如下所示

(char)dimension

将通过ASCII码为您提供符号。

您输入了什么?输出是什么?一个整数。当我输入10时,结果是49。我试过打字,但它保持49。你的代码工作正常。你读的正是一个字符。如果你想读入10,那么你应该使用ReadLine()这当然不能解决你的代码有很多可能的问题。你输入了什么?输出是什么?一个整数。当我输入10时,结果是49。我试过打字,但它保持49。你的代码工作正常。你读的正是一个字符。如果你想读入10,那么你应该使用ReadLine(),这当然不能解决你的代码有很多可能出现的问题。
在现实生活中,你应该使用Int32.TryParse()并处理异常
,但
TryParse
不会抛出,这就是问题所在。@Joren,
exception
显然不是该上下文中使用的合适词。我已经更新了我的答案,提供了我提到的“真实世界”部分。我会做:
if(Int32.TryParse())
检查转换是否成功,然后输出结果,因为输入可以<0@Reniuz,您可以根据需求采取许多不同的方式(OP中没有真正说明)。这只是一个让他们朝正确方向思考的快速示例。我知道:)我给了你一票,但在评论中添加了注释。在现实生活中,你应该使用Int32.TryParse()并处理异常,但
TryParse
不会抛出,这就是问题所在。@Joren,
exception
显然不是该上下文中使用的合适词。我已经更新了我的答案,提供了我提到的“真实世界”部分。我会做:
if(Int32.TryParse())
检查转换是否成功,然后输出结果,因为输入可以<0@Reniuz,您可以根据需求采取许多不同的方式(OP中没有真正说明)。这只是一个让他们朝着正确方向思考的简单例子。我知道:)我给了你一票,但在评论中加了一条说明就是这样。
int a = 0;
if(Int32.TryParse(Console.ReadLine(), out a))
{
    // Do your calculations with 'a' 
}
else
{
    // Some warnings
}