当我运行代码时,最终输出是 Rent for apartment # 204 is $System.Int32[]
。我的问题的重点是getRent
方法。当我进入代码时,for 循环函数会找到用户输入apartmentNumber
,然后运行单元数组以匹配索引。所以当它进入printit
方法时,它仍然显示用户输入apartmentNumber
。
如何匹配相应的租金指数?
static void Main(string[] args)
{
int[] apartment = { 123, 204, 601, 609, 612 }; // initialize apartment array
int[] rent = {500, 750, 495, 800, 940 }; // intitialize rent array
int apartmentNumber = 0; // declare apartmentNumber int
getRent (apartment , rent, apartmentNumber ); /*pass the apartment and rent
array by ref and apartment number console entry */
Console.WriteLine("Press any key to continue..."); // Lets user know program over
Console.ReadKey(); // keeps the program from closing quickly after executing
}
static void getRent(int [] apartment, int [] rent, int apartmentNumber) //method getRent
{
Console.WriteLine ("Enter apartment number"); // query user to enter apartment number
apartmentNumber = int.Parse (Console.ReadLine ()); // convert apartment number to int
for (int i = 0; i < apartment.Length - 1; i++) // for loop to match apartmentNumber
if (apartment[i] == apartmentNumber)// match it to apartment array
Console.WriteLine(); // space between user input and output
printit(apartmentNumber, rent);
}
static void printit (int apartmentNumber, int [] rent) // static method printit
{
string string1 = "Rent for apartment #" + apartmentNumber;
string string2 = " is $" + rent;
StringBuilder mystring = new StringBuilder();
mystring.Append(string1);
mystring.Append(string2);
Console.WriteLine(mystring);
}