我做错了什么?/新手?学习编程

本文关键字:新手 学习 编程 错了 什么 | 更新日期: 2023-09-27 18:09:55

我试图在控制台中创建一个简单的单位数计算器。(实际上我只关心乘法)

所以,这是我的代码,如果有人可以帮助我。

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

编辑:澄清一下,我知道0*13=0(是的,它之前说的是12,实际上是13)。问题是它不允许我设置input1,而只是将其设置为13并继续执行。

编辑2:我想对Matt说声谢谢,因为他所做的改变使代码能够正确工作。那么,看起来我的第一个代码除了告诉你自己的名字之外还有别的作用

我做错了什么?/新手?学习编程

Console.Read()只读取一个字符。因此,您的第一个Console.Read()读取您的数字,第二个读取CR Ascii码(13),因为您的"Enter"。使用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"键设置input。按"Enter"键读取"input1"。只是不要按回车键;相反,立即一个接一个地输入两个数字。最好的解决方法是System.Console.ReadLine

System.Console.Read从控制台中读取单个字符。当你输入0键时,你的程序实际上读取字符'0'('0' == 48在ASCII中),而不是数字0。

我是这样修改你的程序的:

不使用读取一个字符的System.Console.Read,我会使用读取整行而不是单个键/字符的System.Console.ReadLine。这将允许您的用户输入大于1位的数字。您可以使用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();
    }
}

这与Read()和ReadLine()的工作方式有关。试试ReadLine()

我认为这是因为您的程序使用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();
    }
}

可能你需要一个c#专家来回答,但是当我调试时,我得到了这个答案,因为当你输入时,它会推到控制台13和10。因此,您需要两次Read来通过它们。

只需复制粘贴到你的代码

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();
}
输出:

请输入一个数字并按回车键:1
您已输入:49,13,10