我正在编写一个程序,用学生ID #字段、学生姓名字段和学生姓字段获取学生信息的数据。用户将输入每个学生(最多20名学生)的数据,或直到用户输入“999”的学生ID字段。接下来,我想根据姓氏字段将学生信息排序为两个独立的桶。
我对水桶的正确分离有问题。我将使用CompareTo方法来比较学生姓氏数组的姓氏字符串,但是当我打印桶时,它们是混在一起的。例如,以and开头的姓氏应该进入“低值”桶,以J-Z开头的姓氏应该进入“高值”桶。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _123_Assignment2
{
using System;
using static System.Console;
class Program
{
struct student
{
public int studentId;
public string firstName;
public string lastName;
};
static void Main(string[] args)
{
student[] studentInfo = new student[20];
string[] bucketLow = new string[20];
string[] bucketHigh = new string [20];
int x = 0;
int y = 0;
int z = 1;
WriteLine("Enter student ID number:");
studentInfo[x].studentId = Convert.ToInt32(ReadLine());
while (studentInfo[x].studentId != 999)
{
WriteLine("Enter first name:");
studentInfo[x].firstName = ReadLine();
WriteLine("Enter last name:");
studentInfo[x].lastName = ReadLine();
x++;
WriteLine("Enter student ID number:");
studentInfo[x].studentId = Convert.ToInt32(ReadLine());
}
for (int j = 0; j < x; j++)
{
if(studentInfo[j].lastName.CompareTo(studentInfo[z].lastName)<= 0)
bucketLow[y] = studentInfo[j].lastName;
else
bucketHigh[y] = studentInfo[j].lastName;
y++;
z++;
}
WriteLine("Unsorted Table:");
for (int j = 0; j < studentInfo.Length; j++)
{
WriteLine("{0}{1}{2}",studentInfo[j].studentId,studentInfo[j].firstName,
studentInfo[j].lastName);
}
WriteLine("Bucket 1:");
for (int j = 0; j < x; j++)
{
WriteLine(bucketLow[j]);
}
WriteLine("Bucket 2:");
for (int j = 0; j < x; j++)
{
WriteLine(bucketHigh[j]);
}
}
}}
我相信我没有正确地编写CompareTo方法,我试着分别从数组的开头和结尾进行排序,并得到相同的结果?
发布于 2017-02-27 18:21:07
我不认为你排序有效(j和z比较):
studentInfo[j].lastName.CompareTo(studentInfo[z].lastName)<= 0试着简化桶的排序--如果你知道你的桶是A和J,也许你应该替换以下部分:
for (int j = 0; j < x; j++)
{
if(studentInfo[j].lastName.CompareTo(studentInfo[z].lastName)<= 0)
bucketLow[y] = studentInfo[j].lastName;
else
bucketHigh[y] = studentInfo[j].lastName;
y++;
z++;
}
}试一试如下:
for (var i = 0; i < studentInfo.Length; i++)
{
if (studentInfo[i].lastName[0] <= 'K')
bucketLow[y] = studentInfo[i].lastName;
else
bucketHigh[y] = studentInfo[i].lastName;
y++;
}(当然,还要添加一些检查,以确定至少有一个字符的有效输入,等等)
发布于 2017-02-27 18:07:22
由于这似乎是家庭作业,我将不实际为您编写正确的代码。但这里至少有一些你的问题:
A开始,通过K ),另一个表示姓氏(从L开始,通过Z ),那么您需要与K或L进行比较,以确定正确的桶。与其他名称相比,将数据随机化。类似于string.Compare(studentInfo[j].lastName, "L", StringComparison.CurrentCultureIgnoreCase) < 0的东西应该能工作。IndexOutOfRangeException崩溃,因为您增加了x并将ID值存储到数组中,而没有检查是否输入了20个学生的数据。在输入了20名学生之后,即使用户输入了999,while条件也不会检查,直到为时已晚,并且代码已经尝试将值存储到数组中。也许还有其他的问题,这些是我乍一看就注意到的问题。
作为以后的参考,您在这里询问堆栈溢出时,应该确保您提供了一个好的Minimal, Complete, and Verifiable code example。你接近了;至少代码已经完成了。但是不要让其他用户输入您的测试数据。编写一个单独的程序,其中包含所有内置的数据,没有用户提示,也不做任何不严格要求重现问题的操作。
发布于 2017-02-27 19:35:42
如果必须使用结构和数组,则可以考虑下面的代码,以便将名称分离到适当的桶中。正如我所评论的,您需要两个索引,每个桶一个索引。因为要将数组大小固定为20,所以在输出结果时,如果小于20,则会出现空行。
x = studentInfo.Length;
int lowIndex = 0;
int highIndex = 0;
for (int j = 0; j < x; j++) {
if (String.CompareOrdinal(studentInfo[j].lastName, "L") < 0) {
bucketLow[lowIndex] = studentInfo[j].lastName;
lowIndex++;
} else {
bucketHigh[highIndex] = studentInfo[j].lastName;
highIndex++;
}
}https://stackoverflow.com/questions/42492483
复制相似问题