C# 方法从数组A询问问题并将相应的答案存储在数组b中(3个字符串和1个双字符串)
Newb/hobbiest在这里试图增加我对数组的理解,为此,我正在尝试构建一个小的膳食计算器,它将用户数据存储在一个数组中,该数组包含三个字符串和一个双精度字符串(用户的名字、用户的姓氏、餐厅的名称,以及一个双精度的膳食成本)。我知道有比我尝试的方法简单得多的方法,但我的目的是在我开始了解循环和方法之后,增加对数组工作方式的了解(我迷路了/害怕阵列,所以我强迫自己整个周末都在阵列上工作,从今晚开始-漫长的周末,我希望这将是一个值得学习的周末:D) 我目前正在研究一种填充数组的方法,其工作原理如下: 调用GetUserData(询问问题); GetUserData要求用户提供特定类型的数据(字符串,除非最后一个位置[3],否则它是一个双精度值,稍后我将对此进行计算) GetUserData循环调用arrQuestions[]以询问用户问题 GetUserData将用户答案读取到arrUserData[]中的相应位置 arrUserData[]已填充(此时使用四个字符串,我在进行数学运算时必须将四个字符串转换为双精度字符串,我希望这是可行的) 简而言之,我正在尝试建立一种方法,从一个数组中询问一系列问题,并将答案存储在另一个数组中,然后我可以用它来做各种奇怪的事情,因为我试图更好地理解数组,以及如何使用它们,它们的优点和缺点 我向你们所有人保证,我已经试着用谷歌搜索了这个问题的答案,我已经阅读了包括和stackOverflow在内的多个地方的数组,但是我找到的答案并不是以我的技能水平的人理解的方式写的/我的理解不是这样的,我无法从我迄今所读到的内容中猜出答案 以下代码都位于“program”类中C# 方法从数组A询问问题并将相应的答案存储在数组b中(3个字符串和1个双字符串),c#,arrays,loops,methods,overloading,C#,Arrays,Loops,Methods,Overloading,Newb/hobbiest在这里试图增加我对数组的理解,为此,我正在尝试构建一个小的膳食计算器,它将用户数据存储在一个数组中,该数组包含三个字符串和一个双精度字符串(用户的名字、用户的姓氏、餐厅的名称,以及一个双精度的膳食成本)。我知道有比我尝试的方法简单得多的方法,但我的目的是在我开始了解循环和方法之后,增加对数组工作方式的了解(我迷路了/害怕阵列,所以我强迫自己整个周末都在阵列上工作,从今晚开始-漫长的周末,我希望这将是一个值得学习的周末:D) 我目前正在研究一种填充数组的方法,其工作原理如
public void Play()
{
GetString("+ + + Meal Calculator Exercise + + +");
String command = "";
String[] arrQuestions = new String[3];//questions asked
arrQuestions [0] = "First Name: ";//string back
arrQuestions [1] = "Last Name: ";//string back
arrQuestions [2] = "Restaurant Name: ";//string back
arrQuestions [3] = "Cost of Meal: ";//I want a double back for this question
String[] arrUserData = new String[3];/user answers stored
arrUserData[0] = " ";//string
arrUserData[1] = " ";//string
arrUserData[2] = " ";//string
arrUserData[3] = " ";//figure out how to convert to double
do {
GetString("+ + + Meal Calculator Exercise + + +");
GetUserData(arrQuestions[i]);//run loop, ask questions populate arrUserData array
GetString("Again? ");
command = Console.ReadLine().ToLower().Trim();
Console.Clear();
}
while (command == "y" || command == "yes");
GetString("+ + + Thank you + + + Have a wonderful time + + + Goodbye! + + +");
}
public String GetString(String strTxt) {
Console.WriteLine(strTxt);return Console.ReadLine();}
public Array GetUserData(String strTxt)
{
for (int i = 0; i < arrUserData.Length; i++)
Console.WriteLine(arrQuestion[i]);
return Console.ReadLine(arrUserData[i]);
}
static void Main(string[] args){
Program myProgram = new Program();
myProgram.Play();}
public void Play()
{
GetString(“+++膳食计算器练习+++”);
String命令=”;
String[]arrQuestions=新字符串[3];//询问的问题
arrQuestions[0]=“名字:;//返回字符串
arrQuestions[1]=“姓氏:;//返回字符串
arrQuestions[2]=“餐厅名称:;//返回字符串
arrQuestions[3]=“餐费:;//这个问题我要双倍退款
String[]arrUserData=新字符串[3];/存储了用户答案
arrUserData[0]=“”;//字符串
arrUserData[1]=“”;//字符串
arrUserData[2]=“”;//字符串
arrUserData[3]=“”;//了解如何转换为双精度
做{
GetString(“+++膳食计算器练习+++”);
GetUserData(arrQuestions[i]);//运行循环,询问问题填充arrUserData数组
GetString(“又来了?”);
command=Console.ReadLine().ToLower().Trim();
Console.Clear();
}
而(command==“y”| | command==“yes”);
GetString(“+++谢谢++++玩得开心++++再见!+++”;
}
公共字符串GetString(字符串strText){
Console.WriteLine(strText);返回Console.ReadLine();}
公共数组GetUserData(字符串strText)
{
for(int i=0;i
在查看您的代码时,我将尝试推荐一些我认为使用了您所展示的一些现有知识的东西
我不会深入研究代码,但我会尝试解释:
我可以计算数组中有多少个问题,(array.length)然后我会做一个while循环,检查回答的问题数量是否等于数组长度。在这个循环中,您可以使用console readline将问题逐个放入数组,并递增计数器变量,然后使用递增的变量将答案插入到用户数据数组中
这有意义吗?如果您需要任何澄清,或者这不是您想要的。请让我知道。因此,更多的研究、挠头和好心人的帮助会让我得出以下结论:
class Program
{
String[] arrUserData = new String[4];// Must specify 4 in brackets for array length, not the same as an index
// it will fill up as items are added to it starting from position 0 automatically
// should also be at class level for multiple method access
public void Play()
{
String command; // doesn't need to have an empty string value, only be declared
String[] arrQuestions = new String[4];//questions asked
arrQuestions[0] = "First Name: ";
arrQuestions[1] = "Last Name: ";
arrQuestions[2] = "Restaurant Name: ";
arrQuestions[3] = "Cost of Meal: ";
do {
GetString("+ + + Meal Calculator Exercise + + +"); // I removed the one in the top of the method, or else it does it twice
// pass in array of questions to satisfy necessary array argument in GetUserData();
GetUserData(arrQuestions);//run loop, ask questions populate arrUserData array
command = GetString("Again? "); // your GetString method returns whatever the console reads,
// so it can be assigned to your command variable at the same time, or else the user has to put in y or yes twice
Console.Clear();
}
while (command == "y" || command == "yes");
GetString("+ + + Thank you + + + Have a wonderful time + + + Goodbye! + + +");
}
public String GetString(String strTxt)
{
Console.WriteLine(strTxt); return Console.ReadLine().ToLower().Trim();
}
// changed it to a void to it just simply assigns the values to the class level array
public void GetUserData(string[] thisArray)
{
for (int i = 0; i < thisArray.Length; i++)//use the passed in array to determine loop length
{// missing curly braces
Console.WriteLine(thisArray[i]); // write question with corresponding index to console
arrUserData[i] = Console.ReadLine(); // use Console.ReadLine to get input and assign it to corresponding index in userData
if (i == 3) // check for the last index to convert to double
{// here's the basic way, can cause a lot of errors if user input is not a double
Convert.ToDouble(arrUserData[3]);
// here's the way trapping all possible errors, and giving a nice message for each
// remove the other Convert.ToDouble method and uncomment this to try giving some false values to see how it works
/*
try
{
Convert.ToDouble(arrUserData[3]);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert " + arrUserData[3] + " to a Double.");
}
catch (OverflowException)
{
Console.WriteLine(arrUserData[3] + " is outside the range of a Double.");
}
* */
}
}
SeeUserData(thisArray); // to display the user data, demonstrates nesting methods that can
// operate off of one single variable pass in your Play method
// you could also move it to your play method and change it to SeeUserData(arrQuestions); and it would work the same
}
public void SeeUserData(string[] sameArrayAgain)
{
Console.WriteLine("Here's the data for " + arrUserData[0]);
for (int i = 0; i < sameArrayAgain.Length; i++)
{
Console.WriteLine("Your " + sameArrayAgain[i] + " " + arrUserData[i]);
}
}
static void Main(string[] args) { Program myProgram = new Program(); myProgram.Play(); }
}
类程序
{
String[]arrUserData=新字符串[4];//必须在括号中为数组长度指定4,与索引不同
//当项目从位置0开始自动添加到它时,它将填充
//对于多方法访问,也应该在类级别
公共游戏
{
String命令;//不需要有空字符串值,只需声明
String[]arrQuestions=新字符串[4];//询问的问题
ARR问题[0]=“名字:”;
问题[1]=“姓氏:”;
问题[2]=“餐厅名称:”;
问题[3]=“餐费:”;
做{
GetString(“+++膳食计算器练习+++”;//我删除了方法顶部的一个,否则它会执行两次
//传入问题数组以满足GetUserData()中必需的数组参数;
GetUserData(arrQuestions);//运行循环,询问问题填充arrUserData数组
command=GetString(“再次?”);//您的GetString方法返回控制台读取的内容,
//因此,它可以同时分配给您的命令变量,否则用户必须输入y或yes两次
Console.Clear();
}
而(command==“y”| | command==“yes”);
GetString(“+++谢谢++++玩得开心++++再见!+++”;
}
公共字符串GetString(字符串strText)
{
Console.WriteLine(strText);return Console.ReadLine().ToLower().Trim();
}
//将其更改为void,以便只将值分配给类级数组
公共无效GetUserData(