英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论