需要帮助以单独的方法访问数组的特定值
Need help accessing the specific value of an array in a seperate method
我需要帮助在单独的方法中访问数组的特定值。在这个程序中,我应该编写一个程序,要求用户输入日历年每个月的降雨量。该程序随后将确定年度总降雨量和平均每月降雨量,最后将允许用户搜索特定数量。
我应该为最后一个方法执行此操作:SearchForAmounts()
– 此方法将是一个 void 方法,并且有两个双精度数组作为参数
列表,一个用于降雨量数组,一个用于带有月份的数组。它会要求用户在数组中搜索降雨量。然后它将报告该数量在哪个月首次下降或者它将
报告说那一年没有下那么大的雨。它将使用 Array class 方法之一来确定要输出的内容。它会在方法中执行两次 - 即会在方法中重复代码两次。
我无法找到一种方法来访问 monthInTheYear
数组中的值。这是我的代码:
namespace CJohnson_Prog8
{
class Program
{
static void Main(string[] args)
{
string[] monthInTheYear;
double[] rainfallAmmounts;
monthInTheYear = new string[12] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
rainfallAmmounts = new double[12];
Console.WriteLine("Welcome to the Annual Rainfall Program");
GetRainFall(rainfallAmmounts, monthInTheYear);
PrintAmounts(rainfallAmmounts, monthInTheYear);
TotalYearRain(rainfallAmmounts);
SearchForAmounts(rainfallAmmounts, monthInTheYear);
}
public static void GetRainFall(double[] rainfall, string[] month)
{
for (int i = 0; i < rainfall.Length; i++)
{
Console.Write("Enter the rainfall in inches in {0}: ", month[i]);
rainfall[i] = double.Parse(Console.ReadLine());
while (rainfall[i] < 0)
{
Console.Write("Enter the rainfall in inches in {0}: ", month[i]);
rainfall[i] = double.Parse(Console.ReadLine());
}
}
}
public static void PrintAmounts(double[] rainfall, string[] month)
{
for (int i = 0; i < rainfall.Length; i++)
{
Console.WriteLine("{0}\t\t{1:F2}", month[i], rainfall[i]);
}
}
public static double TotalYearRain(double[] totalrainFall)
{
double averageRainFall;
double sum = 0;
for (int i = 0; i < totalrainFall.Length; i++)
{
sum += totalrainFall[i];
}
averageRainFall = sum / 12;
Console.WriteLine("");
Console.WriteLine("Total Annual Rain is: {0:F2}", sum);
Console.WriteLine("Average Month Rain: {0:F2}", averageRainFall);
return sum;
}
public static void SearchForAmounts(double[] searchRainFall, string[] searchMonths)
{
Console.Write("Enter a rainfall amount to search for: ");
double RainFall = double.Parse(Console.ReadLine());
if (Array.IndexOf(searchRainFall, RainFall) != 1)
{
Console.WriteLine(searchMonths[i] + " was the first month to have " + RainFall + " inches fell this year.");
}
else
{
Console.WriteLine("No rainfall of " + RainFall + " inches fell that year.");
}
}
}
}
for (int i = 0; i <= 1; i++) {
//do stuff
var index = Array.IndexOf(searchRainFall, RainFall);
if (index > -1) {
Console.WriteLine(searchMonths[index] + " was the first month to have " + RainFall + " inches fell this year.");
} else {
Console.WriteLine("No rainfall of " + RainFall + " inches fell that year.");
}
}
我需要帮助在单独的方法中访问数组的特定值。在这个程序中,我应该编写一个程序,要求用户输入日历年每个月的降雨量。该程序随后将确定年度总降雨量和平均每月降雨量,最后将允许用户搜索特定数量。
我应该为最后一个方法执行此操作:SearchForAmounts()
– 此方法将是一个 void 方法,并且有两个双精度数组作为参数
列表,一个用于降雨量数组,一个用于带有月份的数组。它会要求用户在数组中搜索降雨量。然后它将报告该数量在哪个月首次下降或者它将
报告说那一年没有下那么大的雨。它将使用 Array class 方法之一来确定要输出的内容。它会在方法中执行两次 - 即会在方法中重复代码两次。
我无法找到一种方法来访问 monthInTheYear
数组中的值。这是我的代码:
namespace CJohnson_Prog8
{
class Program
{
static void Main(string[] args)
{
string[] monthInTheYear;
double[] rainfallAmmounts;
monthInTheYear = new string[12] { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
rainfallAmmounts = new double[12];
Console.WriteLine("Welcome to the Annual Rainfall Program");
GetRainFall(rainfallAmmounts, monthInTheYear);
PrintAmounts(rainfallAmmounts, monthInTheYear);
TotalYearRain(rainfallAmmounts);
SearchForAmounts(rainfallAmmounts, monthInTheYear);
}
public static void GetRainFall(double[] rainfall, string[] month)
{
for (int i = 0; i < rainfall.Length; i++)
{
Console.Write("Enter the rainfall in inches in {0}: ", month[i]);
rainfall[i] = double.Parse(Console.ReadLine());
while (rainfall[i] < 0)
{
Console.Write("Enter the rainfall in inches in {0}: ", month[i]);
rainfall[i] = double.Parse(Console.ReadLine());
}
}
}
public static void PrintAmounts(double[] rainfall, string[] month)
{
for (int i = 0; i < rainfall.Length; i++)
{
Console.WriteLine("{0}\t\t{1:F2}", month[i], rainfall[i]);
}
}
public static double TotalYearRain(double[] totalrainFall)
{
double averageRainFall;
double sum = 0;
for (int i = 0; i < totalrainFall.Length; i++)
{
sum += totalrainFall[i];
}
averageRainFall = sum / 12;
Console.WriteLine("");
Console.WriteLine("Total Annual Rain is: {0:F2}", sum);
Console.WriteLine("Average Month Rain: {0:F2}", averageRainFall);
return sum;
}
public static void SearchForAmounts(double[] searchRainFall, string[] searchMonths)
{
Console.Write("Enter a rainfall amount to search for: ");
double RainFall = double.Parse(Console.ReadLine());
if (Array.IndexOf(searchRainFall, RainFall) != 1)
{
Console.WriteLine(searchMonths[i] + " was the first month to have " + RainFall + " inches fell this year.");
}
else
{
Console.WriteLine("No rainfall of " + RainFall + " inches fell that year.");
}
}
}
}
for (int i = 0; i <= 1; i++) {
//do stuff
var index = Array.IndexOf(searchRainFall, RainFall);
if (index > -1) {
Console.WriteLine(searchMonths[index] + " was the first month to have " + RainFall + " inches fell this year.");
} else {
Console.WriteLine("No rainfall of " + RainFall + " inches fell that year.");
}
}