C# 如何在我的方法程序中处理3个参数?
我正试图为下面所写的问题编写一个C# 如何在我的方法程序中处理3个参数?,c#,arrays,methods,parameters,C#,Arrays,Methods,Parameters,我正试图为下面所写的问题编写一个C#代码,但无论我做什么,我都不知道如何解决这个问题。我想我不明白为什么要这样做!无论如何,这就是我需要做的:启动一个新的控制台应用程序 在Main方法中,创建一个大小为5的字符串数组,命名该数组 轮廓要求用户输入他们的名字,但您必须这样做 通过调用方法,您将编写名为GetStringInput的 方法应该需要3个参数 第一个是字符串,这是要询问用户的问题。(对于第一种用法,您可以传入一个字符串,上面写着“输入您的名字”) 第二个参数是配置文件数组 第三个参数
C#
代码,但无论我做什么,我都不知道如何解决这个问题。我想我不明白为什么要这样做!无论如何,这就是我需要做的:启动一个新的控制台应用程序
- 在Main方法中,创建一个大小为5的字符串数组,命名该数组
轮廓要求用户输入他们的名字,但您必须这样做
通过调用方法,您将编写名为
的 方法应该需要3个参数GetStringInput
- 第一个是字符串,这是要询问用户的问题。(对于第一种用法,您可以传入一个字符串,上面写着“输入您的名字”)
- 第二个参数是
配置文件
数组
- 第三个参数是一个int,即索引,也就是说,用户的答案应该存储在数组中的哪个位置
- 第一个问题(名字)
- 通过调用相同的
,但传递不同的问题,再次询问用户的姓氏GetStringInput
- 通过调用相同的
,再次询问用户的年龄, 但是传递一个不同的问题(注意年龄将被输入并存储为字符串,而不是转换为int)GetStringInput
- 通过调用相同的
,但传递不同的问题,再次询问用户最喜欢的颜色GetStringInput
- 通过调用相同的
,但传递不同的问题,再次询问用户最喜欢的运动GetStringInput
DisplayProfile
的新方法。此方法应该需要一个字符串数组作为参数,您将传入配置文件数组。添加一个Console.ReadLine()代码>语句,您的主方法完成
GetStringInput
和DisplayProfile
。DisplayProfile
方法应该从数组中提取信息,并以合理的方式写出值
比如说,你的名字叫约翰·史密斯,你21岁了。你最喜欢的颜色是绿色,你最喜欢的运动是网球static void Main(string[] args)
{
string[] Profile = new string[5];
GetUserInput(Profile);
DisplayProfile(Profile);
}
private static void DisplayProfile(string[] Showrofile)
{
Console.WriteLine("Your name is {0} {1}, you are {2} years old.\nYour favorite color is {3} and your favorite sport is {4}.",
Showrofile[0], Showrofile[1], Showrofile[2], Showrofile[3], Showrofile[4]);
}
public static string [] GetUserInput(string[] Getprofile)
{
string[] Question = new string[5];
Question[0] = "Enter your First name:";
Question[1] = "Enter your Last name:";
Question[2] = "Enter your Age:";
Question[3] = "Enter your Favorite color:";
Question[4] = "Enter your Favorite sport:";
for (int i = 0; i < Question.Length; i++)
{
Console.WriteLine(Question[i]);
Getprofile[i] = Console.ReadLine();
}
return Getprofile;
}
}
}
static void Main(字符串[]args)
{
字符串[]配置文件=新字符串[5];
GetUserInput(配置文件);
显示配置文件(Profile);
}
私有静态void DisplayProfile(字符串[]Showrofile)
{
Console.WriteLine(“您的名字是{0}{1},您是{2}岁。\n您最喜欢的颜色是{3},您最喜欢的运动是{4}”,
Showrofile[0]、Showrofile[1]、Showrofile[2]、Showrofile[3]、Showrofile[4]);
}
公共静态字符串[]GetUserInput(字符串[]Getprofile)
{
字符串[]问题=新字符串[5];
问题[0]=“请输入您的名字:”;
问题[1]=“输入您的姓氏:”;
问题[2]=“输入您的年龄:”;
问题[3]=“输入您最喜欢的颜色:”;
问题[4]=“输入您最喜欢的运动:”;
for(int i=0;i
我不知道该怎么办。我感谢你的帮助 我一直在看评论,其他人都是对的,你应该看看如何提问,因为不清楚。看起来一些简单的谷歌搜索很容易回答这个问题,但我会同情你的头发拉扯。这是一个你要求的例子 有时候,最好的方法是走开,然后平静地回来,因为当你问Gabe如何用三个参数来制作一个方法时,你几乎回答了你自己的问题。请看下面,祝你好运,慢慢来
public static void Main()
{
const int TOTAL_QUESTIONS = 5;
string[] profile = new string[TOTAL_QUESTIONS];
string[] questions = new string[]
{
"Enter your First name:",
"Enter your Last name:",
"Enter your Age:",
"Enter your Favorite color:",
"Enter your Favorite sport:"
};
for (int i = 0; i < TOTAL_QUESTIONS; i++)
{
profile = GetUserInput(questions[i], profile, i);
}
DisplayProfile(profile);
}
private static void DisplayProfile(string[] profile)
{
Console.WriteLine($"Your name is {profile[0]} {profile[1]}, you are {profile[2]} years old.\nYour favorite color is {profile[3]} and your favorite sport is {profile[4]}.");
}
//Here is where you put the parameters you told Gabe about.
private static string[] GetUserInput(string question, string[] profile, int index)
{
Console.WriteLine(question);
profile[index] = Console.ReadLine();
return profile;
}
publicstaticvoidmain()
{
const int TOTAL_问题=5;
string[]profile=新字符串[TOTAL_QUESTIONS];
字符串[]问题=新字符串[]
{
“输入您的名字:”,
“输入您的姓氏:”,
“输入您的年龄:”,
“输入您最喜欢的颜色:”,
“输入您最喜爱的运动:”
};
对于(int i=0;i
编辑:
因为你在老师的评论中提到GetUserInput不需要返回任何东西,所以你可以看到它是如何工作的。看起来是这样的
public static void Main()
{
const int TOTAL_QUESTIONS = 5;
string[] profile = new string[TOTAL_QUESTIONS];
string[] questions = new string[]
{
"Enter your First name:",
"Enter your Last name:",
"Enter your Age:",
"Enter your Favorite color:",
"Enter your Favorite sport:"
};
for (int i = 0; i < TOTAL_QUESTIONS; i++)
{
GetUserInput(questions[i], profile, i);
}
DisplayProfile(profile);
}
private static void DisplayProfile(string[] profile)
{
Console.WriteLine($"Your name is {profile[0]} {profile[1]}, you are {profile[2]} years old.\nYour favorite color is {profile[3]} and your favorite sport is {profile[4]}.");
}
private static void GetUserInput(string question, string[] profile, int index)
{
Console.WriteLine(question);
profile[index] = Console.ReadLine();
}
publicstaticvoidmain()
{
const int TOTAL_问题=5;
string[]profile=新字符串[TOTAL_QUESTIONS];
字符串[]问题=新字符串[]
{
“输入您的名字:”,
“输入您的姓氏:”,
“输入您的年龄:”,
“输入您最喜欢的颜色:”,
“输入您最喜爱的运动:”
};
对于(int i=0;i