我坚持这个计划,任何帮助将不胜感激。
有3种方法:inputPhone,outputPhones和Main。 inputPhone在循环中运行并接受用户输入。此信息应存储在数组中。在循环中断之前,用户将继续输入信息。如果损坏,将显示输入的每个电话。
我正在使用ref参数。我需要将这些值存储在Main方法中,然后通过该方法传递数组。
我只是想朝着正确的方向努力;一个例子会很棒。
编辑:问题已经解决。非常感谢你的输入人员。我会尽快添加你的建议来清理我的代码,让它运行得更好。
答案 0 :(得分:2)
首先,我会创建一个单独的类来存储您的手机数据:
public class Phone
{
public string Manufacturer { get; set; }
public string Model { get; set; }
public bool HasCord { get; set; }
public double Price { get; set: }
}
然后代替:
static void inputPhone(ref string manufacturer, ref string model, ref bool hasCord, ref double price)
你可以:
static Phone GetPhone()
您可以在GetPhone方法中创建一个Phone实例,并返回用适当数据填充的对象。
而不是:
double[] prices = new double[100];
string[] manufacturers = new string[100];
string[] models = new string[100];
bool[] hasCords = new bool[100];
然后你可以:
List<Phone> phones = new List<Phone>();
然后在每次调用GetInput(之前:inputPhone)之后将其添加到列表中:
phones.Add(GetPhone());
然后将outputPhones更改为:
static void DisplayPhones(List<Phone> phones)
所以你的所有代码都是这样的:
static Phone GetPhone()
{
Phone phone = new Phone();
Console.Write("Enter the phone manufacturer: ");
phone.Manufacturer = Console.ReadLine();
Console.Write("Enter the phone model: ");
phone.Model = Console.ReadLine();
Console.Write("Is it cordless? [Y or N]: ");
phone.HasCord = Console.ReadLine().ToUpper() == "Y";
Console.Write("Enter the phone price: ");
phone.Price = Convert.ToDouble(Console.ReadLine());
return phone;
}
static void DisplayPhones(List<Phone> phones)
{
for (int i = 0; i < phones.Count; i++)
{
Console.WriteLine("==Phone #{0}==", i);
Console.WriteLine("Phone Manufacturer: {0}", phones[i].Manufacturer);
Console.WriteLine("Phone Model: {0}", phones[i].Model);
Console.WriteLine("Has Cord: {0}", phones[i].HasCord ? "Yes" : "No");
Console.WriteLine("Phone Price: {0}", phones[i].Price);
}
Console.WriteLine("Number of phones entered: {0}", phones.Count);
}
static void Main(string[] args)
{
List<Phone> phones = new List<Phone>();
bool shouldContinue = true;
do
{
phones.Add(GetPhone());
Console.Write("Would like to process another phone? [Y or N]: ", shouldContinue);
shouldContinue = Console.ReadLine().ToUpper() == "Y";
} while (shouldContinue == true);
if (shouldContinue == false)
{
DisplayPhones(phones);
}
}
答案 1 :(得分:1)
制作该类Phone的所有thos数组属性,您只需要在该类中访问它们。除此之外,你可以使用out
ref
的{{1}}并且不知道在运行代码时初始化所有内容。
答案 2 :(得分:1)
您需要使用array[numberOfPhones]
访问数组中的特定项,然后使用ref array[numberOfPhones]
传递它。
do
{
inputPhone(ref manufacturers[numberOfPhones],
ref models[numberOfPhones],
ref hasCords[numberOfPhones],
ref prices[numberOfPhones]);
numberOfPhones++; // Increase the number of phones afterwards
Console.Write("Would like to process another phone? [Y or N]: ", Continue);
Continue = Console.ReadLine().ToUpper() == "Y";
} while (Continue == true);
答案 3 :(得分:1)
重写此代码以利用List<Phone>
代替数组。正如您所看到的,与基于数组的方法相比,整个代码被简化了许多
class Phone
{
// Property to store the fields of a Phone object
public string Manifacturer {get;set;}
public string Model {get;set;}
public string IsCordless {get;set;}
public decimal Price {get;set;}
// No parameters to pass, but returns a Phone instance
static Phone inputPhone()
{
Phone p = new Phone();
Console.Write("Enter the phone manufacturer: ");
p.Manufacturer = Console.ReadLine();
Console.Write("Enter the phone model: ");
p.Model = Console.ReadLine();
Console.Write("Is it cordless? [Y or N]: ");
p.IsCordless = Console.ReadLine().ToUpper() == "Y";
Console.Write("Enter the phone price: ");
p.Price = Convert.ToDecimal(Console.ReadLine());
return p;
}
// Pass the list and loop on every element
static void outputPhones(List<Phone> pList)
{
foreach (Phone o in pList)
{
Console.WriteLine("Phone Manufacturer: {0}", p.Manufacturer);
Console.WriteLine("Phone Model: {0}", p.Model);
Console.WriteLine("Has Cord: {0}", p.IsCordless ? "Yes" : "No");
Console.WriteLine("Phone Price: {0}", p.Price);
}
Console.WriteLine("Number of phones entered: {0}", pList.Count);
}
static void Main(string[] args)
{
// Initialize an empty Phone list
List<Phone> pList = new List<Phone>();
bool continueLoop = true;
do
{
// Get the input from the user and add to the list
Phone p = inputPhone();
pList.Add(p);
Console.Write("Would like to process another phone? [Y or N]: ", continueLoop);
continueLoop = Console.ReadLine().ToUpper() == "Y";
} while (continueLoop == true);
// output the list
outputPhones(pList);
}
}
答案 4 :(得分:1)
为什么不让手机上课:
public class Phone
{
public double Price {get;set;}
public string Manufacturer {get;get;}
public string Model {get;set;}
public bool HasCord{get;set;}
public override string ToString()
{
return string.Format("Phone Manufacturer: {0}\nPhone Model {1}\nHas Cord: {2}\nPhone Price{3}", this.Manufacturer, this.Model, this.HasCord ? "Yes" : "No", this.Price);
}
}
然后,您可以将程序更改为面向对象,如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace newProg
{
class YourApp
{
private Phone inputPhone()
{
Console.Write("Enter the phone manufacturer: ");
string manufacturer = Console.ReadLine();
Console.Write("Enter the phone model: ");
string model = Console.ReadLine();
Console.Write("Is it cordless? [Y or N]: ");
bool hasCord = Console.ReadLine().ToUpper() == "Y";
Console.Write("Enter the phone price: ");
double price = Convert.ToDouble(Console.ReadLine());
return new Phone {
Manufacturer = manufacturer,
Model = model,
HasCord = hasCord,
Price = price;
};
}
static void outputPhones(List<Phone> phones)
{
foreach (var index = 0; index < phones.Length; index++)
{
Console.WriteLine("==Phone #{0}==", index);
Console.WriteLine(phone.ToString());
}
Console.WriteLine("Number of phones entered: {0}", phones.Length);
}
static void Main(string[] args)
{
List<Phone> phones = new List<Phone>();
bool Continue = true;
do
{
phones.Add(inputPhone());
Console.Write("Would like to process another phone? [Y or N]: ", Continue);
Continue = Console.ReadLine().ToUpper() == "Y";
} while (Continue == true);
if (Continue == false)
{
outputPhones(phones);
}
}
}