【发布时间】:2014-06-08 17:38:13
【问题描述】:
所以我有另一个任务,我正在尝试使用 ArrayList 对象来编译姓氏列表,获取计数,按升序排序,然后按降序排序。我遇到的问题是,Visual Studio 说我去编译/调试时出现错误,但没有任何标记,我似乎无法弄清楚问题出在哪里。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Lab5_exB
{
class Program
{
static void Main(string[] args)
{
string anotherList;
do
{
ArrayList lastNames = new ArrayList();
string exitValue;
do
{
Console.WriteLine("Enter a last name..");
exitValue = Console.ReadLine();
if (exitValue == "N" || exitValue == "n")
{
break;
}
lastNames.Add(exitValue);
} while (exitValue != "N" || exitValue != "n");
Console.WriteLine("Amount of last names entered: " + lastNames.Count);
lastNames.Sort();
Console.WriteLine("Last names in Ascending Alphabetical Order");
Console.WriteLine("------------------------------------------");
int i = 0;
while (i < lastNames.Count)
{
Console.WriteLine(lastNames);
i++;
}
lastNames.Reverse();
Console.WriteLine("Last names in Descending Alphabetical Order");
Console.WriteLine("-------------------------------------------");
int z = 0;
while (z < lastNames.Count)
{
Console.WriteLine(lastNames);
z++;
}
Console.WriteLine("Would you like to enter another list? (Y/N)");
anotherList = Convert.ToString(Console.Read());
}while (anotherList == "Y" || anotherList == "y");
Console.Read();
}
}
}
我使用单独的函数编写了我的代码,并将它们全部拼凑成一个混乱的方法/函数。上面是乱七八糟的。这是单独的功能:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Lab5_exB
{
class Program
{
public static int GetLastNames(ArrayList lastNames, ref string exitValue)
{
do
{
Console.WriteLine("Enter a last name..");
exitValue = Console.ReadLine();
if (exitValue == "N" || exitValue == "n")
{
break;
}
lastNames.Add(exitValue);
} while (exitValue != "N" || exitValue != "n");
return 0;
}
public static int DisplayArrayNames(ArrayList lastNames)
{
Console.WriteLine("Amount of last names entered: " + lastNames.Count);
lastNames.Sort();
Console.WriteLine("Last names in Ascending Alphabetical Order");
Console.WriteLine("------------------------------------------");
int i = 0;
while (i < lastNames.Count)
{
Console.WriteLine(lastNames);
i++;
}
return 0;
}
public static int ReverseArrayNames(ArrayList lastNames)
{
lastNames.Sort();
lastNames.Reverse();
Console.WriteLine("Last names in Descending Alphabetical Order");
Console.WriteLine("-------------------------------------------");
int z = 0;
while (z < lastNames.Count)
{
Console.WriteLine(lastNames);
z++;
}
return 0;
}
static void Main(string[] args)
{
string anotherList;
do
{
ArrayList lastNames = new ArrayList();
string exitValue;
GetLastNames(lastNames);
DisplayArrayNames(lastNames);
ReverseArrayNames(lastNames);
Console.WriteLine("Would you like to enter another list? (Y/N)");
anotherList = Convert.ToString(Console.Read());
}while (anotherList == "Y" || anotherList == "y");
Console.Read();
}
}
}
使用不同功能时。我收到一个错误,“方法 'GetLastNames' 没有重载需要 1 个参数”,我没有看到这个问题......它似乎写得很好。当编写为一个方法/函数时,没有显示错误,但有 1 个构建错误……我认为这与第一个“函数”中的代码有关。
我在想,可能声明的函数需要设置为字符串,但它们正在标记,因为它们没有返回值,而且我认为我不能返回 ArrayList。
有什么想法吗?
编辑:根据他人的建议稍微更改了我的代码。在构建中仍然收到“未知”1 失败。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace Lab5_exB
{
class Program
{
public static ArrayList GetLastNames()
{
string exitValue;
var lastNames = new ArrayList();
do
{
Console.WriteLine("Would you like to enter another name? (Y/N)");
exitValue = Convert.ToString(Console.Read());
if (exitValue == "N" || exitValue == "n")
{
break;
}
Console.WriteLine("Enter a last name..");
lastNames.Add(Console.ReadLine());
} while (exitValue != "N" || exitValue != "n");
return lastNames;
}
public static void DisplayArrayNames(ArrayList lastNames)
{
Console.WriteLine("Amount of last names entered: " + lastNames.Count);
lastNames.Sort();
Console.WriteLine("Last names in Ascending Alphabetical Order");
Console.WriteLine("------------------------------------------");
int i = 0;
while (i < lastNames.Count)
{
Console.WriteLine(lastNames);
i++;
}
}
public static void ReverseArrayNames(ArrayList lastNames)
{
lastNames.Sort();
lastNames.Reverse();
Console.WriteLine("Last names in Descending Alphabetical Order");
Console.WriteLine("-------------------------------------------");
int z = 0;
while (z < lastNames.Count)
{
Console.WriteLine(lastNames);
z++;
}
}
static void Main(string[] args)
{
string anotherList;
do
{
var lastNames = GetLastNames();
DisplayArrayNames(lastNames);
ReverseArrayNames(lastNames);
Console.WriteLine("Would you like to enter another list? (Y/N)");
anotherList = Convert.ToString(Console.Read());
}while (anotherList == "Y" || anotherList == "y");
Console.Read();
}
}
}
【问题讨论】:
-
你应该使用
List<T>,而不是ArrayList。 -
错误很明显。您的代码有多个冗余元素,请删除它们。从
int GetLastNames(ArrayList lastNames, ref string exitValue)中的第二个参数开始。它也不需要返回值。 -
@Slaks:你看过练习描述了吗?
-
错误信息的哪一部分你不明白?
-
@HenkHolterman:如果他的练习告诉他使用非泛型集合,他应该改用更好的课程。
标签: c# arrays arraylist reference