我是c#的新手(我从一月份开始),我需要帮助处理我写的代码。在出现错误的所有行上,它说
期望的类,委托,枚举,接口或结构
这是我的代码:
using System;
namespace pawlowski_Catherine_Lab3
{
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
}
public Course ()
{
this.hours="3.00";
}
public Course(string description, string prefix, double number, double hours)
{
this.description=description;
this.prefix=prefix;
this.number=number;
this.hours=hours;
}
public override string ToString ()
{
return string.Format ("Course: "+prefix+"\nCourse Number: "+number+"\nDescription: "+description+"\nCredit hours: "+hours);
}
}
差不多,错误在哪里,除了第一个之外,在开头就是公开的。请帮我弄清楚我做错了什么,以便将来我自己解决。
答案 0 :(得分:2)
这可能会被关闭,所以:
这里有你的(格式化):
using System;
namespace pawlowski_Catherine_Lab3 {
public class Course {
protected string description;
protected string prefix;
protected double number;
protected double hours;
}
public Course() {
this.hours = "3.00";
}
public Course(string description, string prefix, double number, double hours) {
this.description = description;
this.prefix = prefix;
this.number = number;
this.hours = hours;
}
public override string ToString() {
return string.Format("Course: " + prefix + "\nCourse Number: " + number + "\nDescription: " + description + "\nCredit hours: " + hours);
}
}
这里有你应该拥有的东西(构造函数和方法是类的一部分):
using System;
namespace pawlowski_Catherine_Lab3 {
public class Course {
protected string description;
protected string prefix;
protected double number;
protected double hours;
public Course() {
this.hours = "3.00";
}
public Course(string description, string prefix, double number, double hours) {
this.description = description;
this.prefix = prefix;
this.number = number;
this.hours = hours;
}
public override string ToString() {
return string.Format("Course: " + prefix + "\nCourse Number: " + number + "\nDescription: " + description + "\nCredit hours: " + hours);
}
}
}
正确的缩进是你的朋友。
答案 1 :(得分:2)
您应该将所有方法和属性放在类中 声明。
类似的东西:
using System;
namespace pawlowski_Catherine_Lab3
{
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
public Course ()
{
this.hours="3.00";
}
public Course(string description, string prefix, double number, double hours)
{
this.description=description;
this.prefix=prefix;
this.number=number;
this.hours=hours;
}
public override string ToString ()
{
return string.Format ("Course: "+prefix+"\nCourse Number: "+number+"\nDescription: "+description+"\nCredit hours: "+hours);
}
}
}
答案 2 :(得分:1)
在这里:
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
} // Get rid of this line here
public Course ()
{
...
答案 3 :(得分:1)
在类范围内声明您的承包商和方法。
替换您的代码 用这个
using System;
namespace pawlowski_Catherine_Lab3
{
public class Course
{
protected string description;
protected string prefix;
protected double number;
protected double hours;
public Course ()
{
this.hours="3.00";
}
public Course(string description, string prefix, double number, double hours)
{
this.description=description;
this.prefix=prefix;
this.number=number;
this.hours=hours;
}
public override string ToString ()
{
return string.Format ("Course: "+prefix+"\nCourse Number: "+number+"\nDescription: "+description+"\nCredit hours: "+hours);
}
}
}