2017-02-22 5 views
0

Мне нужна помощь в написании программы, которая принимает информацию о студенте для трех отдельных полей (ID Number, first name, last name). Затем сортирует таблицу по алфавиту в соответствии с полем last name. Пользователь будет вводить данные студента, тогда я хотел бы, чтобы он разделил данные last name на два ведра, которые затем будут помещены в сортировку пузырьков. У меня возникают проблемы с добавлением данных в отдельные ведра.Запись сортировки ковша в C# с использованием строк в качестве значений

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[0]; 
      string[] bucketHigh = new string [0]; 
      int x = 0; 
      int y = 0; 

      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 < studentInfo.Length; j++) 
      {  
       if (studentInfo[j].lastName.CompareTo(studentInfo[j + 1].lastName) > 0) 
        bucketLow[y] = studentInfo[j].lastName; 
       else 
        bucketHigh[y] = studentInfo[j].lastName; 
       y++; 
      }             
     } 
    } 
} 
+1

если вы не делать домашнее задание, подумайте ** Linq ** –

+0

' "У меня возникли проблемы"' - Что беда? В частности, в чем проблема? – David

+0

Если вы не делаете домашнее задание, и вы на самом деле пишете свой собственный пузырь, вы делаете это неправильно. –

ответ

0

Попробуйте этот код, вы можете проверить свои комментарии для объяснения:

 student[] studentInfo = new student[20]; 

     //Make sure you initialize the correct number of variables on your string 
     string[] bucketLow = new string[20]; 
     string[] bucketHigh = new string[20]; 
     int x = 0; 
     int y = 0; 

     //I commented out this line since you are already asking for inputs on without going on your counter scenario 

     //Console.WriteLine("Enter student ID number:"); 
     //studentInfo[x].studentId = Convert.ToInt32(Console.ReadLine()); 

     //I made changes on this line, since, you are doing a condition based on the number of increment on your counter 
     while (x <= 2) 
     { 
      //I put the enter student ID above since it will not be counted if it was put after the counter x 
      Console.WriteLine("Enter student ID number:"); 
      studentInfo[x].studentId = Convert.ToInt32(Console.ReadLine()); 
      Console.WriteLine("Enter first name:"); 
      studentInfo[x].firstName = Console.ReadLine(); 
      Console.WriteLine("Enter last name:"); 
      studentInfo[x].lastName = Console.ReadLine(); 
      x++; 

     } 

     for (int j = 0; j < x; j++) 
     { 
      //Make sure you put group all your conditions on round brackets to achieve your desired condition 
      if ((studentInfo[j].lastName.CompareTo(studentInfo[j + 1].lastName)) > 0) 
       bucketLow[y] = studentInfo[j].lastName; 
      else 
       bucketHigh[y] = studentInfo[j].lastName; 
      y++; 
     } 
+0

Спасибо, это привело меня на правильный путь, который я искал. – CoreyC

+0

Теперь вы можете принять это как ответ, если это ответит на ваш вопрос :) @CoreyColgan –

Смежные вопросы