我能使用这种用户输入方式调用方法吗?

huangapple go评论53阅读模式
英文:

Am I unable to call a method with this style of User Input?

问题

static void Main(string[] args)
{
    int number1, number2;

    Console.WriteLine("请输入一个数字:");
    number1 = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("请输入另一个数字:");
    number2 = Convert.ToInt32(Console.ReadLine());

    Console.WriteLine("答案是:" + Add(number1, number2));
    Console.ReadLine();
}

public static int Add(int num1, int num2)
{
    return num1 + num2;
}
英文:

I am trying to call a method with the inputs stated within Main. I have seen that some people get user inputs from another method besides Main but wanted to complete it this way. Is it possible?

I am able to input data when asked as stated. Although, "Your answer is: " returns nothing.

 static void Main(string[] args)
        {
            int number1, number2;

            Console.WriteLine("Please enter a number: ");
            number1 = Convert.ToInt32(Console.ReadLine());
            

            Console.WriteLine("Please enter another number: ");
            number2 = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Your answer is: ", Add(number1, number2));
            Console.ReadLine();
        }

        public static int Add(int num1, int num2)
        {
            return num1 + num2;
        }
    }
}

答案1

得分: 1

问题在于您的文本“Your answer is:”中没有任何字符串格式标记。尝试添加 {0},就像这样:Your answer is: {0},甚至可能是 {0} is your answer.

有关更多信息,请参阅https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-8.0#system-console-writeline(system-string-system-object)。

英文:

The problem is you don't have any string format tokens in your text Your answer is:. Try adding {0} like this: Your answer is: {0} or maybe even {0} is your answer.

See https://learn.microsoft.com/en-us/dotnet/api/system.console.writeline?view=net-8.0#system-console-writeline(system-string-system-object) for more information.

huangapple
  • 本文由 发表于 2023年4月20日 01:03:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057108.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定