我正在尝试在控制台中为单位数字创建一个简单的计算器。(实际上我只关心乘法)
所以,这是我的代码,如果有人可以帮助我。
class multiplythisnumber
{
static void Main()
{
int input, input1, output; //variable decleration
System.Console.WriteLine("This application is meant to multiply two single digit numbers.");
System.Console.WriteLine("You can choose both numbers, however this is only a test.\r I am not sure the read command even does what I think it does.");
System.Console.WriteLine();
System.Console.Write("Please enter the first number: ");
input = System.Console.Read(); //Reads my input + 48(assuming 48 is the value of enter)
input = input - 48;
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
input1 = System.Console.Read(); //Doesn't wasit for input and sets input1 = 13
System.Console.WriteLine();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
}
我添加了评论来向其他人解释我在做什么,就像个人笔记一样。
最后一行总是以“(0)乘以13等于0”结束。 - 假设我输入1使用零。
编辑: 只是为了澄清我知道0 * 13 = 0(是的它早先说过12它真正说的是13)。问题是它不允许我设置input1而只是将其设置为13并继续执行。
Edit2:我想感谢Matt,因为他所做的更改允许代码正常工作。所以,看起来我的第一个代码实际上除了告诉你自己的名字之外还有其他的东西。
答案 0 :(得分:4)
Console.Read()只读取一个字符。所以你的第一个Console.Read()读取你的号码,第二个读取CR Ascii代码(13)因为你的“回车”。 请改用ReadLine()。
System.Console.Write("Please enter the first number: ");
string first = System.Console.ReadLine();
input = Convert.ToInt32(first);
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
string second = System.Console.ReadLine();
input1 = Convert.ToInt32(second);
System.Console.WriteLine();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
答案 1 :(得分:1)
问题是你要一个接一个地读两个字。按解决这个问题的最佳方法是input
键时会设置1
。按Enter键时会读取input1
。只是不要按Enter键;而是一个接一个地立即输入两个数字。System.Console.ReadLine
System.Console.Read
从控制台读取单个字符。键入0键时,程序实际上会读取字符“0”(ASCII中的“0”== 48)而不是数字0。
以下是我修复程序的方法:
我不会使用System.Console.Read
来读取字符,而是使用System.Console.ReadLine
来读取整行而不是单个键/字符。这将允许您的用户输入超过一位数的数字。您可以使用Int32.Parse
class multiplythisnumber
{
static void Main()
{
int input, input1, output; //variable decleration
System.Console.WriteLine("This application is meant to multiply two single digit numbers.");
System.Console.WriteLine("You can choose both numbers, however this is only a test.\r I am not sure the read command even does what I think it does.");
System.Console.WriteLine();
System.Console.Write("Please enter the first number: ");
input = Int32.Parse(System.Console.Read());
//input = input - 48;//No need to do this anymore as we have already converted the user's input to an integer.
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
input1 = Int32.Parse(System.Console.ReadLine());
System.Console.WriteLine();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
}
答案 2 :(得分:1)
它与Read()和ReadLine()的工作方式有关。尝试使用ReadLine()。
答案 3 :(得分:1)
我认为这是因为您的程序使用Console.Read()
而不是Console.ReadLine()
这意味着它一次从输入中读取1个字符。你的第一个输入是5 +一个回报。尝试使用ReadLine,您需要防止输入非整数的东西。您可以使用以下程序来实现您的目标。
class Program
{
static void Main(string[] args)
{
int input, input1;
string output; //variable decleration
System.Console.WriteLine("This application is meant to multiply two single digit numbers.");
System.Console.WriteLine("You can choose both numbers, however this is only a test.\r I am not sure the read command even does what I think it does.");
System.Console.WriteLine();
System.Console.Write("Please enter the first number: ");
int.TryParse(System.Console.ReadLine(),out input); //Reads my input
System.Console.WriteLine();
System.Console.Write("Please enter the second number: ");
int.TryParse(System.Console.ReadLine(), out input1);
System.Console.WriteLine();
if (input != 0 && input1 != 0) {
input = input - 48;
output = (input * input1).ToString();
}else
{
output = "NaN";//not a number
}
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
}
答案 4 :(得分:-1)
可能你需要一个C#专家来回答,但是当我调试时我得到了它,因为当你输入时,它将推送到控制台13和10.所以你需要Read
两次通过它们。< / p>
static void Main()
{
int input, input1, output; //variable decleration
System.Console.WriteLine("This application is meant to multiply two single digit numbers.");
System.Console.WriteLine("You can choose both numbers, however this is only a test.\r I am not sure the read command even does what I think it does.");
System.Console.WriteLine();
System.Console.Write("Please enter the first number: ");
input = System.Console.Read(); //Reads my input + 48(assuming 48 is the value of enter)
input = input - 48;
/**
*HINT: Try to use below double line:
*/
System.Console.Read();
System.Console.Read();
System.Console.Write("Please enter the second number: ");
input1 = System.Console.Read(); //Doesn't wasit for input and sets input1 = 13
System.Console.Read();
output = input * input1;
System.Console.WriteLine();
System.Console.Write("{0} times {1} equals {2}.", input, input1, output);
System.Console.ReadKey();
}
您可以尝试查看更多内容:
static void Main()
{
Console.Write("Please enter just one number and press enter:");
int input1 = Console.Read();
int input2 = Console.Read();
int input3 = Console.Read();
Console.WriteLine("You've inputted: {0},{1},{2}",input1, input2, input3);
Console.ReadKey();
}
请输入一个号码,然后按enter键:1
你输入了:49,13,10