简体   繁体   English

C# 控制台:在 char 变量中输入变量后,如何在 LastName 中输入值?

[英]C# Console : How can i enter a value in LastName after i Enter a variable in the char variable?

Here's my code:这是我的代码:

public static void Main(string[] args) 
{
  string FirstName;
  string LastName;

  char MiddleInitial;

  Console.Write("Enter  first name: ");

  FirstName = Console.ReadLine();

  if (FirstName.Equals("null")) 
  {
    Console.Write("Insert middle initial: ");
    Console.Write("\n Insert last name: \n");
  } 
  else 
  {
    Console.Write("Insert middle initial: ");

    MiddleInitial = (char) Console.Read();

    Console.Write("Enter  last name: ");

    LastName = Console.ReadLine();

    Console.ReadKey();
  }

}

I can't enter the lastname variable at the last part of my code.我无法在代码的最后部分输入姓氏变量。 Anyone there more knowledgable who can answer my concern?那里有更多知识渊博的人可以回答我的问题吗?

The output always went like this: Enter first name:Jason Insert middle initial: f Enter last name: output 总是这样: 输入名字:Jason 插入中间名首字母:f 输入姓氏:

After i press one letter it goes back to the IDE

The program output should be like this:程序output应该是这样的:

Enter  first name:Joshua 

Insert middle initial:F

Enter  last name:Capili

Enter  first name:null 
Insert middle initial:
Enter  last name:

I try to execute your code, in my VS it works.我尝试执行您的代码,在我的 VS 中它有效。 Try this code:试试这个代码:

 MiddleInitial = Console.ReadKey().KeyChar;

Or this:或这个:

 MiddleInitial = Console.ReadLine()[0];

This is happening because Console.Read() appends a new line to the input .发生这种情况是因为Console.Read() 将新行附加到 input When you subsequently do Console.Readline() there is already a line in the buffer so it sets LastName to whatever comes after the first character you type, even if it is empty string.当您随后执行Console.Readline()时,缓冲区中已经有一行,因此它将LastName设置为您键入的第一个字符之后的任何内容,即使它是空字符串也是如此。

You can use ReadLine() for the middle initial (as a string) and just take the first character.您可以将ReadLine()用于中间首字母(作为字符串)并只取第一个字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我怎么能放一个规则,只允许您在C#的控制台应用程序中输入数字 - how can i put a rule that will only let you enter a number in a console application on C# 如何在 foreach 循环中为 char 变量赋值? C# - How can i assign a value to a char variable in a foreach loop? C# 在textBox中输入索引后如何选择listBox的项目c# - how i can selected item of listBox after enter the index in textBox c# 为什么在输入新的char值后while循环关闭 - Why while loop closes after I enter new char value 在 C# 中,我输入一个字符,然后输出一些东西。 但是在输入控制后不等待输入并直接给出输出 - In C# I am taking a char input and then I output something. But after input control is not waiting for enter and straightaway giving output 我输入到TextBox中的数据未分配给UWP应用中的value变量 - The data I enter into a TextBox is not being assigned to the value variable in a UWP app 如何使用 C# 将变量设置为至少最小值 - How can I set a variable to be at least a minimum value with C# 如何将列值保存到SQL C#中的变量? - How can I save a column value to a variable in SQL C#? 在 C# 中,如何在运行时创建值类型变量? - In C#, how can I create a value type variable at runtime? 如何在C#中将MySqlCommand的值存储到本地变量? - How can I store the value of a MySqlCommand in C# to a local variable?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM