我有一个电脑任务,我被困在了。 我必须列出大学课程,让用户添加或删除该列表。我有基本列表和我的printmenu循环,但无法弄清楚如何将用户输入到classlist中的另一个类。
到目前为止,这是我的main.cs ....
using System;
using System.Collections.Generic;
namespace Lab04
{
class MainClass
{
public static string prefix;
public static int coursenumber;
public static int credithours;
public static string coursedescription;
public static void Main (string[] args) //creates 4 courses (used some leftover Lab 03 data and some new)
{
Course c1 = new Course();
Course c2 = new Course("ENGL", 1102, 3, "Second level class for English Composition.");
Course c3 = new Course("GEOG", 1113, 4, "This is an online lab course for Geography.");
Course c4 = new Course("BWV", 101, 2, "This is basket weaving on a college level, for those of you who need a break.");
Console.WriteLine ("Here are your current courses for this semester:");
List<Course> classlist = new List<Course>();
classlist.Add (c1);
classlist.Add (c2);
classlist.Add (c3);
classlist.Add (c4);
foreach(Course c in classlist)
Console.WriteLine(c + " ");
Console.WriteLine();
int choice;
do {
PrintMenu();
choice = Int32.Parse (Console.ReadLine());
ProcessChoice(choice);
} while (choice != 4);
Console.WriteLine ("Thank you for using this system.");
}
public static void PrintMenu() //print menu for user input
{
Console.WriteLine("Main Menu:");
Console.WriteLine("1. Add a Course to the list.");
Console.WriteLine("2. Delete a Course from the list.");
Console.WriteLine("3. Print your class schedule.");
Console.WriteLine("4. Exit this System.");
Console.WriteLine("Please enter your numerical choice.");
}
static void ProcessChoice(int c) // do while loop code for user input
{
if (c == 1)
{
Console.WriteLine ("Please enter the Course prefix.");
prefix = Console.ReadLine();
Console.WriteLine ("Please enter the Course number.");
coursenumber = Int32.Parse(Console.ReadLine());
Console.WriteLine ("How many credit hours is this course worth?");
credithours = Int32.Parse(Console.ReadLine());
Console.WriteLine ("Please enter a short course description.");
coursedescription = Console.ReadLine();
}
else if (c == 2)
{
Console.WriteLine ("Which course would you like to remove?");
}
else if (c == 3)
{
Console.WriteLine ("Displaying current class schedule.");
}
}
}
}
我查看了这篇文章Adding multiple user input to a List c#,但由于它只是添加了整数,而不是列表中的整个类,所以它没有回答我的问题
答案 0 :(得分:0)
有多种方法。一种方法是使classlist成为全局和
public void ProcessChoice(int c)
{
if (c == 1)
{
//Get the input from console
classlist.Add(new Course{
prefix = prefix,
coursenumber = coursenumber,
credithours = credithours,
coursedescription = coursedescription
});
}
}