2014-01-24 144 views
0

我被困在这个程序和任何帮助,将不胜感激。C#传递参数给阵列

有3种方法:inputPhone,outputPhones和Main。 inputPhone在循环中运行并接受用户输入。这些信息应该存储在一个数组中。在循环被破坏之前,用户将继续输入信息。如果损坏,将显示每个输入的电话。

我正在使用参数参数。我需要将这些值存储在Main方法中,然后通过该方法传递数组。

我只是在正确的方向寻找推动;一个例子会很棒。

编辑:问题已解决。非常感谢你的投入。我会尽快添加你的建议来清理我的代码,让它运行得更好一些。

+6

只是一个建议 - '继续'是C#中的关键字,我会建议命名该变量有些不同(例如'shouldContinue')。 –

+0

好点,它的工作原理,但我会改变它。感谢指针。 :) – fdsa

+0

没问题,只是值得考虑的事情! –

回答

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); 
+0

完美!我知道我很接近,我无法克服最后一步。非常感激。它会让我接受你的回应。 – fdsa

+0

尽管这在技术上会起作用,但我认为其他答案显示出更好更简单的设计。 –

+2

我同意它可以做得更容易,但问题是通过变量作为参考,而不是重新设计他的代码。 – matth

1

制作该类的所有thos数组属性Phone,您将只需在该类的任何位置访问它们。除此之外,您可以使用out insted ref,并且不会意识到初始化运行代码的所有内容。

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方法中并返回填充了适当数据的对象。

相反的:

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()); 

然后将输出电话更改为:

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); 
    } 
} 
+0

这很有道理。就像我在下面回答的那样,我并不知道List选项;对于C#我还是比较新,所以这对未来的项目很有帮助。谢谢:) – fdsa

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); 

    } 
} 
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); 
     } 
    } 
} 
+0

我从来不知道List,但似乎是一个简化一切的好方法。 – fdsa

+0

列表可能是C#中最有用的数据结构。 'System.Collections'命名空间中还有许多其他有用的数据结构。我很高兴能够提供帮助。 –