2014-11-02 12 views
1

我的程序工作正常,除了一个问题“System.String []”即时通讯新编码在C#这是我在大学期间我的第一个主要任务。我问了几个朋友的以及教师(这是无济于事,因为他们只是说,如果这是与任务做的话,他们不会帮助)我不知道我在代码中出错的地方。基本上名称输入在列表中正常可以显示,但数组的第二部分只是想出了“System.String []”,但我觉得它有一个与下面的代码有问题,因为我一直在玩它试图看看什么是错的但没有任何东西可以拿出来。将信息放置在我的数组上给了我“System.String []”,其中数组的其他部分提供了所需的信息。 c#

static void AddDetails() 
    { 
     string nameInput; 
     string licensePlateInput; 
     string parkingSlot; 
     Console.WriteLine("Enter the guest's name"); 
     nameInput = Console.ReadLine(); 
     Console.WriteLine("enter the guest's license plate"); 
     licensePlateInput=Console.ReadLine(); 
     parkingSlot=Convert.ToString(licensePlate); 

     int nextAvailableSlot = FindSlot(""); 
     if (nextAvailableSlot != -1) 
     { 
      name[nextAvailableSlot] = nameInput; 
      licensePlate[nextAvailableSlot] = parkingSlot; 
     } 
     else 
     { 
      Console.WriteLine("No empty spots remain"); 
     } 
    } 

这是我的代码的一部分,允许用户输入所需的详细信息,然后有它保存。

static int FindSlot(string names) 
    { 
     int result = -1; 
     for (int index = 0; index < name.Length;index ++) 
     { 
      if (name[index] == names) 
      { 
       result = index; 
       break; 
      } 
      else if(names == "" && name[index] == null) 
      { 
       result = index; 
       break; 
      } 
     } 

     return result; 

这是我觉得我应该补充的第一部分的扩展,因为我认为它可能有所帮助。

static void ListCars() 
    { 
     for(int index= 0; index < name.Length; index ++) 
     { 
      if(name [index] != "" && name[index] != null) 
      Console.WriteLine(" name {0} car is parked in {1}", name[index], licensePlate[index]); 
     } 
    } 

这段代码是

+0

“系统.String []“是在'String'数组上调用'ToString'的输出,显然这就是你在某处做的事情。如果你真的向我们展示了产生输出的部分,但至少你知道要寻找什么,它可能会很有用:在某处你将数组本身转换为String而不是从中获取String其中一个元素。 – jmcilhinney 2014-11-02 08:49:11

+0

“他们无法帮助,如果它是做任务'在这里诚实的荣誉,但是......如果连你的老师拒绝帮助,这不表示你完全完成这项任务你自己?无论如何,从代码中可以清楚地看到为什么你得到了字符串:你将一个'string []'对象的实例转换为一个字符串 - 即'Convert.ToString(licensePlate)' - 和默认转换在这种情况下,只是给你的类型名称。但是你打算在那里打字的意思我不知道。 – 2014-11-02 08:49:47

+0

它看起来像你想要做的是找到具有车牌阵列的索引(基于'parkingSlot = Convert.ToString(licensePlate);',但正如其他人指出,这将无法正常工作。你需要搜索数组,看看它是否已经有车牌,并且它是什么索引。 – Tim 2014-11-02 08:56:56

回答

0

我带你去一个刺在这一点,虽然我不知道我完全把握问题的输出(这我确定很多人都知道我的)。我认为是扔东西了,就是这条线:

parkingSlot = Convert.ToString(licensePlate); 

正如你已经找到了,这样的结果是System.String[],这是不是你想要的。您的电话已到达下一个可用的时间段,请拨打FindSlot。我想,如果我读正确的代码,你真正想要做的是存储阵列licensePlate的牌照,所以只需使用值,可以在licensePlateInput存储用户输入:

// Remove this line, as it servers no discernible purpose 
//parkingSlot=Convert.ToString(licensePlate); 

int nextAvailableSlot = FindSlot(""); 
if (nextAvailableSlot != -1) 
{ 
    name[nextAvailableSlot] = nameInput; 
    // Use the value the user gave for the license plate 
    licensePlate[nextAvailableSlot] = licensePlateInput; 
} 
+0

我刚刚测试过它,它工作。我想这只是我的错误,我没有意识到是一个错误,直到现在开始。非常感谢Tim – Tyrian 2014-11-02 09:43:52

+0

乐意提供帮助。祝你的作业和快乐的编码:) – Tim 2014-11-02 09:48:07

相关问题