本文介绍了Console.Read不返回我的int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I don't get why my integer isn't coming out correctly, Console.Read() method says it's returning an integer, why isn't WriteLine displaying it correctly?

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


推荐答案

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

Console.Read() only returns the first character of what was typed. You should be using 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()

MSDN-

MSDN - Console.ReadLine()

这篇关于Console.Read不返回我的int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 10:12