Console.Read() and Console.ReadLine() problems

假装没事ソ 提交于 2019-11-26 14:38:02

问题


I have been trying to use Console.Read() and Console.ReadLine() in C# but have been getting weird results. for example this code

Console.WriteLine("How many students would you like to enter?");
int amount = Console.Read();
Console.WriteLine("{0} {1}", "amount equals", amount);

for (int i=0; i < amount; i++)
{
     Console.WriteLine("Input the name of a student");
     String StudentName = Console.ReadLine();
     Console.WriteLine("the Students name is " + StudentName);
}

has been giving me that amount = 49 when I input 1 for the number of students, and Im not even getting a chance to input a student name.


回答1:


This because you read a char. Use appropriate methods like ReadInt32() that takes care of a correct conversion from the read symbol to the type you wish.

The reason why you get 49 is because it's a char code of the '1' symbol, and not it's integer representation.

char     code
0 :      48
1 :      49
2:       50
...
9:       57

for example: ReadInt32() can look like this:

public static int ReadInt32(string value){
      int val = -1;
      if(!int.TryParse(value, out val))
          return -1;
      return val;
}

and use this like:

int val = ReadInt32(Console.ReadLine());

It Would be really nice to have a possibility to create an extension method, but unfortunately it's not possible to create extension method on static type and Console is a static type.




回答2:


Try to change your code in this way

int amount;
while(true)
{
    Console.WriteLine("How many students would you like to enter?"); 
    string number = Console.ReadLine(); 
    if(Int32.TryParse(number, out amount))
        break;
}
Console.WriteLine("{0} {1}", "amount equals", amount); 
for (int i=0; i < amount; i++) 
{ 
    Console.WriteLine("Input the name of a student"); 
    String StudentName = Console.ReadLine(); 
    Console.WriteLine("the Students name is " + StudentName); 
} 

Instead to use Read use ReadLine and then check if the user input is really an integer number using Int32.TryParse. If the user doesn't input a valid number repeat the question.
Using Console.Read will limit your input to a single char that need to be converted and checked to be a valid number.

Of course this is a brutal example without any error checking or any kind of safe abort from the loops.




回答3:


For someone who might still need this:

static void Main(string[] args)
{
     Console.WriteLine("How many students would you like to enter?");
     var amount = Convert.ToInt32(Console.ReadLine());

     Console.WriteLine("{0} {1}", "amount equals", amount);

     for (int i = 0; i < amt; i++)
     {
         Console.WriteLine("Input the name of a student");
         String StudentName = Console.ReadLine();
         Console.WriteLine("the Students name is " + StudentName);
     }
}



回答4:


you get a character char from read not an int. you will need to make it a string first and parse that as a string. THe implementation could look like the below

    Console.WriteLine("How many students would you like to enter?");
    var read = Console.ReadLine();
    int amount;
    if(int.TryParse(read,out amount)) {
      Console.WriteLine("{0} {1}", "amount equals", amount);

      for (int i=0; i < amount; i++)
      {
        Console.WriteLine("Input the name of a student");
        String StudentName = Console.ReadLine();
        Console.WriteLine("the Students name is " + StudentName);
      }
    }

I've changed it to use readline because readline returns a string an doesn't arbitrarily limits the number of students to 9 (the max number with one digit)




回答5:


Console.Read() is returning the char code of the character that you enter. You need to use Convert.ToChar(amount); to get the character as a string, and then you will need to do int.Parse() to get the value you're looking for.




回答6:


Console.Read returns the asci value of the key character that was pressed.

If you use Console.ReadKey().KeyChar you'll get a char that represents the actual character that was pressed.

You can then turn that character to a one character string by using .ToString().

Now that you have a string you can use int.Parse or int.TryParse to turn a string containing entirely numeric characters into an integer.

So putting it all together:

int value;
if (int.TryParse(Console.ReadKey().KeyChar.ToString(), out value))
{
    //use `value` here
}
else
{
    //they entered a non-numeric key
}



回答7:


Instead of:

int amount = Console.Read();

try:

int amount = 0;
int.TryParse(Console.ReadLine(), out amount);

Its because You read just character code, so for example typing 11 You will still get 49. You need to read string value and the parse it to int value. With code above in case of bad input You will get 0.




回答8:


Try this:

int amount = ReadInt32();

or if it doesn't work try this:

int amount = Console.ReadInt32();

or this:

int amount = Convert.ToInt32(Console.Readline());

In this, it will read string then it will convert it into Int32 value.

You can also go here: http://www.java2s.com/Tutorials/CSharp/System.IO/BinaryReader/C_BinaryReader_ReadInt32.htm

If nothing works, please let me know.




回答9:


Console.WriteLine("How many students would you like to enter?");
string amount = Console.ReadLine();
int amt = Convert.ToInt32(amount);

Console.WriteLine("{0} {1}", "amount equals", amount);

for (int i = 0; i < amt; i++)
{
    Console.WriteLine("Input the name of a student");
    String StudentName = Console.ReadLine();
    Console.WriteLine("the Students name is " + StudentName);
}
//thats it



回答10:


TL;DR; Enter key in Windows isn't a single character. This fact is at the root of many issues related to Console.Read() method.

Complete Details: If you run below piece of code on your computer then you can solve lot of mysteries behind Console.Read():

static void Main(string[] args)
{
    var c = Console.Read();
    Console.WriteLine(c);
    c = Console.Read();
    Console.WriteLine(c);
}

While running the program, hit the Enter key just once on your keyboard and check the output on console. Below is how it looks:

Interestingly you pressed Enter key just once but it was able to cater to two Read() calls in my code snippet. Enter key in windows emits two characters for a new line character namely carriage return (\r - ASCII code 13) and line feed (\n - ASCII code 10). You can read about it more here in this post - Does Windows carriage return \r\n consist of two characters or one character?



来源:https://stackoverflow.com/questions/12308098/console-read-and-console-readline-problems

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!