在这段代码中,我尝试编写代码以将客户数据保存在数组列表中。我需要保留名字、姓氏、联系电话和付款方式。
public static void Rent()
{
WriteLine("Enter your first name");
input_fname = ReadLine();
WriteLine("Enter your last name");
input_lname = ReadLine();
WriteLine("Enter your contact number");
input_phone = ReadLine();
WriteLine("Enter your payment method");
input_payment = ReadLine();
ArrayList arr_cust = new ArrayList();
for (int i = 0; i <= arr_cust.Count; i++)
{
arr_cust.Add(input_fname + input_lname + input_phone + input_payment);
}
WriteLine("Pease enter 1 to return to previous menu and 0 to exit");
int input = Convert.ToInt32(ReadLine());
bool exit = false;
while (exit == false)
{
if (input == 1)
{
Main();
}
else if (input == 0)
{
WriteLine("Thank you for use Rental E-Bike System");
System.Environment.Exit(0);
}
else
{
}
}
}
我的问题是当我把我需要保留的所有值和我把 1 放回主页之后。接下来,我再次打开 Rent 页面,将第二个值放入数组列表中。下一步,我需要显示我保存在数组列表中的数据。
您的函数在每次调用时都会Rent
创建一个新实例,这就是为什么它总是为空的,它唯一的值是您在函数中插入的值。ArrayList
arr_cust
Rent
您可以将其初始化为函数外部的静态字段,而不是在arr_cust
内部Rent
进行初始化,以便它保留以前的值。
此外,您不需要 for 循环来插入数据。您只插入一次,并且使用 for 循环您将获得该条目的 N 个重复项。N 是上一个项目计数。
public static ArrayList arr_cust = new ArrayList();
public static void Rent(ArrayList arr_cust)
{
WriteLine("Enter your first name");
input_fname = ReadLine();
WriteLine("Enter your last name");
input_lname = ReadLine();
WriteLine("Enter your contact number");
input_phone = ReadLine();
WriteLine("Enter your payment method");
input_payment = ReadLine();
arr_cust.Add(input_fname + input_lname + input_phone + input_payment);
WriteLine("Pease enter 1 to return to previous menu and 0 to exit");
int input = Convert.ToInt32(ReadLine());
bool exit = false;
while (exit == false)
{
if (input == 1)
{
Main();
}
else if (input == 0)
{
WriteLine("Thank you for use Rental E-Bike System");
System.Environment.Exit(0);
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句