我是C#的新手并且一起编程,我正试着让它运行起来。我整天都在阅读有关静态和非静态的内容,并且似乎无法正确阅读。任何和所有的帮助将不胜感激,因为我需要明天晚上工作的代码。以下是主课程和课程课程:
using System;
namespace Lab4
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Welcome to the Course Monitoring System v1.0");
Course.Menu ();
Course.Choice ();
}
}
}
这是课程类:
using System;
using System.Collections.Generic;
namespace Lab4
{
public class Course
{
private string courseNumber;
private string courseName;
private int courseHours;
private string descript;
private string prefix;
public List<Course> school = new List<Course> ();
private int howmany=0;
private int totalcourse=0;
//This section is for returning and adjusting values of private data through the main program.
public string cn{
get {return courseNumber; }
set {if (value != null)
cn = value;
}
}
public string name{
get{ return courseName; }
set{if (value!="")
name=courseName;
}
}
public int hours{
get {return courseHours; }
set {if (value != 0)
hours = value;
}
}
public string script {
get {return descript; }
set {
if (value != "")
script = value;
}
}
public string pfix {
get {return prefix; }
set {if (value != "")
pfix = value;
}
}
public Course (string pfix, string name, string cn, int hours, string script)
{
courseNumber = cn;
courseName = name;
courseHours = hours;
descript= script;
prefix = pfix;
}
//This portion of code overrides the string and allows it to output the information held within the constructor.
public override string ToString ()
{
return prefix + " " + courseName+" " + courseNumber + " " + descript + " It is a " + courseHours + " hour course.";
}
//The menu application for my program
public static void Menu()
{
Console.WriteLine ("Please choose from one of the following options: ");
Console.WriteLine ("......................................................................");
Console.WriteLine ("1.)Start a new Course List.");
Console.WriteLine ("2.)Delete Course from Current List.");
Console.WriteLine ("3.)Print Current Course List.");
Console.WriteLine ("4.)Add Course to Current List.");
Console.WriteLine ("5.)Shutdown Program");
}
//This is the controller for the menu. It allows for functionality to control the tasks requested by the user.
public void Choice()
{
int selection=int.Parse(Console.ReadLine());
while (selection >5 || selection < 1) {
Console.WriteLine ("Choice invalid. Please choose between 1 and 5.");
selection = int.Parse (Console.ReadLine ());
}
if (selection == 4) {
Add ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 2) {
Delete ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 3) {
Print ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 1) {
New ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 5) {
Console.WriteLine ();
Console.WriteLine ("Thank you for using this program for your scheduling needs.");
}
}
//This method when called will print a numbered list of courses refrenced by the created List
public void Print()
{
Console.WriteLine ();
Console.WriteLine ("Course Name.........Course Number.........Course Description........Course Hours");
Console.WriteLine ("********************************************************************************");
for (int i = 0; i < totalcourse; i++) {
int place = i + 1;
Console.WriteLine (place+".)"+school [i]);
}
Console.WriteLine ();
}
//This method will add an item to the end of the current list.
public void Add()
{
Console.WriteLine ();
Console.Write ("How many courses would you like to add at the end of your index?");
int numberAdded = int.Parse(Console.ReadLine ());
for (int i = 0; i < numberAdded; i++) {
Console.WriteLine ("Please use the following templet for your entries...");
Console.WriteLine ("Course Prefix, Course Name, Course Number, Course Hours, Course Description ");
school.Add (new Course (prefix = Console.ReadLine (), courseName = Console.ReadLine (),
courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = Console.ReadLine ()));
}
totalcourse = totalcourse + numberAdded;
Console.WriteLine ();
}
//This method will delete an Item from the list based on the position 0-x based on x-1, the output that is seen by the user. After each iteration it will
//also create a list so that further deletions can be managed approiatly.
public void Delete()
{
if (totalcourse < 1) {
Console.WriteLine ();
Console.WriteLine ("There is nothing to delete!");
Console.WriteLine ();
}else{
Console.WriteLine ();
Console.Write ("How many entries do you wish to remove?");
int removed = int.Parse (Console.ReadLine ());
for (int i = 0; i < removed; i++) {
Console.WriteLine ("Please type the index line number of the item you wish to remove.");
int delete = int.Parse (Console.ReadLine ());
school.RemoveAt (delete - 1);
totalcourse = totalcourse - 1;
Print ();
}
}
Console.WriteLine ();
}
//This method is called to create a new list of Courses to be created by the user.
public void New()
{
Console.WriteLine ();
if (howmany > 0) {
Clear ();
} else {
Console.Write ("How many courses do you want to create? ");
howmany = int.Parse (Console.ReadLine ());
Console.WriteLine ();
for (int i = 0; i < howmany; i++) {
Console.WriteLine ();
school.Add (new Course (prefix = Console.ReadLine (), courseName = Console.ReadLine (), courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = Console.ReadLine ()));
}
totalcourse = totalcourse + howmany;
Console.WriteLine ();
}
}
//If there is already a list in place this method will be called and you will be asked if you wish to delete and start new.
public void Clear()
{
Console.WriteLine ();
Console.Write ("You want to discard old work and start a new list? Enter True or False...:");
bool clean=bool.Parse(Console.ReadLine());
if (clean == true) {
school.Clear ();
totalcourse = 0;
howmany = 0;
New ();
} else
Console.WriteLine ("No changes will be made. Exiting to Main Menu...");
Console.WriteLine ();
}
/* public static void Myfour ()
{
Console.WriteLine ();
Console.WriteLine ("These are four pre loaded courses.");
Course c1 = new Course ("MISSILE", 84, 3, "The Course is for missiles", "CRN");
Console.WriteLine (c1);
Console.WriteLine ();
}*/
}
}
答案 0 :(得分:0)
您的课程课程必须是静态的:
public class Course
您的选择方法也是如此:
public static void Choice()
静态意味着您可以使用类的成员(例如方法)而无需使用“new”创建类的实例。这是你要做的,所以方法和容器类都需要static
修饰符。您需要将“课程”类中的所有其他方法设置为静态。
或者,您为什么不在Main
方法中创建课程类的实例:
Course myCourse = new Course();
myCourse.Menu();
myCourse.Choice();
然后您的代码将起作用
答案 1 :(得分:0)
这应该让你开始。我所做的是
这绝不是完整的,但应该让你开始。
using System;
using System.Collections.Generic;
namespace Lab4
{
public class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("Welcome to the Course Monitoring System v1.0");
var c = new Course();
c.Menu();
c.Choice();
}
}
public class Course
{
private string courseNumber;
private string courseName;
private int courseHours;
private string descript;
private string prefix;
public List<Course> school = new List<Course> ();
private int howmany=0;
private int totalcourse=0;
//This section is for returning and adjusting values of private data through the main program.
public string cn{
get {return courseNumber; }
set {if (value != null)
cn = value;
}
}
public string name{
get{ return courseName; }
set{if (value!="")
name=courseName;
}
}
public int hours{
get {return courseHours; }
set {if (value != 0)
hours = value;
}
}
public string script {
get {return descript; }
set {
if (value != "")
script = value;
}
}
public string pfix {
get {return prefix; }
set {if (value != "")
pfix = value;
}
}
public Course()
{
}
public Course (string pfix, string name, string cn, int hours, string script)
{
courseNumber = cn;
courseName = name;
courseHours = hours;
descript= script;
prefix = pfix;
}
//This portion of code overrides the string and allows it to output the information held within the constructor.
public override string ToString ()
{
return prefix + " " + courseName+" " + courseNumber + " " + descript + " It is a " + courseHours + " hour course.";
}
//The menu application for my program
public void Menu()
{
Console.WriteLine ("Please choose from one of the following options: ");
Console.WriteLine ("......................................................................");
Console.WriteLine ("1.)Start a new Course List.");
Console.WriteLine ("2.)Delete Course from Current List.");
Console.WriteLine ("3.)Print Current Course List.");
Console.WriteLine ("4.)Add Course to Current List.");
Console.WriteLine ("5.)Shutdown Program");
}
//This is the controller for the menu. It allows for functionality to control the tasks requested by the user.
public void Choice()
{
int selection=int.Parse(Console.ReadLine());
while (selection >5 || selection < 1) {
Console.WriteLine ("Choice invalid. Please choose between 1 and 5.");
selection = int.Parse (Console.ReadLine ());
}
if (selection == 4) {
Add ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 2) {
Delete ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 3) {
Print ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 1) {
New ();
Menu ();
Choice ();
Console.WriteLine ();
}
if (selection == 5) {
Console.WriteLine ();
Console.WriteLine ("Thank you for using this program for your scheduling needs.");
}
}
//This method when called will print a numbered list of courses refrenced by the created List
public void Print()
{
Console.WriteLine ();
Console.WriteLine ("Course Name.........Course Number.........Course Description........Course Hours");
Console.WriteLine ("********************************************************************************");
for (int i = 0; i < totalcourse; i++) {
int place = i + 1;
Console.WriteLine (place+".)"+school [i]);
}
Console.WriteLine ();
}
//This method will add an item to the end of the current list.
public void Add()
{
Console.WriteLine ();
Console.Write ("How many courses would you like to add at the end of your index?");
int numberAdded = int.Parse(Console.ReadLine ());
for (int i = 0; i < numberAdded; i++) {
Console.WriteLine ("Please use the following templet for your entries...");
Console.WriteLine ("Course Prefix, Course Name, Course Number, Course Hours, Course Description ");
school.Add (new Course (prefix = Console.ReadLine (), courseName = Console.ReadLine (),
courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = Console.ReadLine ()));
}
totalcourse = totalcourse + numberAdded;
Console.WriteLine ();
}
//This method will delete an Item from the list based on the position 0-x based on x-1, the output that is seen by the user. After each iteration it will
//also create a list so that further deletions can be managed approiatly.
public void Delete()
{
if (totalcourse < 1) {
Console.WriteLine ();
Console.WriteLine ("There is nothing to delete!");
Console.WriteLine ();
}else{
Console.WriteLine ();
Console.Write ("How many entries do you wish to remove?");
int removed = int.Parse (Console.ReadLine ());
for (int i = 0; i < removed; i++) {
Console.WriteLine ("Please type the index line number of the item you wish to remove.");
int delete = int.Parse (Console.ReadLine ());
school.RemoveAt (delete - 1);
totalcourse = totalcourse - 1;
Print ();
}
}
Console.WriteLine ();
}
//This method is called to create a new list of Courses to be created by the user.
public void New()
{
Console.WriteLine ();
if (howmany > 0) {
Clear ();
} else {
Console.Write ("How many courses do you want to create? ");
howmany = int.Parse (Console.ReadLine ());
Console.WriteLine ();
for (int i = 0; i < howmany; i++) {
Console.WriteLine ();
school.Add (new Course (prefix = Console.ReadLine (), courseName = Console.ReadLine (), courseNumber = Console.ReadLine (), courseHours = int.Parse (Console.ReadLine ()), descript = Console.ReadLine ()));
}
totalcourse = totalcourse + howmany;
Console.WriteLine ();
}
}
//If there is already a list in place this method will be called and you will be asked if you wish to delete and start new.
public void Clear()
{
Console.WriteLine ();
Console.Write ("You want to discard old work and start a new list? Enter True or False...:");
bool clean=bool.Parse(Console.ReadLine());
if (clean == true) {
school.Clear ();
totalcourse = 0;
howmany = 0;
New ();
} else
Console.WriteLine ("No changes will be made. Exiting to Main Menu...");
Console.WriteLine ();
}
/* public static void Myfour ()
{
Console.WriteLine ();
Console.WriteLine ("These are four pre loaded courses.");
Course c1 = new Course ("MISSILE", 84, 3, "The Course is for missiles", "CRN");
Console.WriteLine (c1);
Console.WriteLine ();
}*/
}
}
答案 2 :(得分:0)
您班级的第一个问题是Course
类包含List<Course> school
变量。学校是另一个逻辑实体,应该在另一个班级。我认为Course
课程应该是一个自己的课程,没有任何static
成员。所有Console
相关函数和school
变量都应包含在(可能是静态的,如果需要的话)School
类中。
然后您的main
函数会调用
School.Menu();
School.Choice();
答案 3 :(得分:0)
您调用Course.Choice()
className.methoddName()
的方式是用于访问静态方法。这意味着您需要更改
public void Choice() { }
public void Menu() { }
到
public static void Choice() { }
public static void Menu() { }
但是,正如我之前所说,由于Choice()
和Menu()
现在是静态的,因此在这些方法中调用的所有内容也必须是静态的,例如Add()
,{ {1}},Delete()
。
那么静态方法意味着什么? - 你不能(并且不能)实例化它们来访问它们。另一种方法是使用Print()
关键字创建class Course
的实例:
new
现在您可以访问Course cr = new Course();
//This works because Course and MainClass are under the same namespace
//Or else, you will need to do this:
OtherNamespace.Class x = new OtherNamespace.Class() ;
类中的所有非静态方法!像这样:
Course
创建一个实例似乎是解决问题的一种更简单的方法,因为所有方法都是实例。但你应该真正了解什么时候更适合使用哪个。您可以阅读有关here的更多信息。
即使你只问过非静态调用。我真的认为你在cr.Menu();
cr.Choice();
课上有很多改进。你在课堂上聚集了太多东西。例如,您可以创建一个新类,只是为了存储所有属性,例如Course
,cn
,name
等。这里将它们设置为静态属性是个好主意,然后你可以使用hours
在任何地方访问它们。