我的代码有什么问题

本文关键字:问题 什么 代码 我的 | 更新日期: 2023-09-27 18:34:55

我是 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);
    }
}

几乎,错误所在的地方,除了第一个之外,在开始时公开的任何内容上。请帮助我弄清楚我具体做错了什么,以便将来可以自己修复它。

我的代码有什么问题

这可能会被关闭,所以:

以下是您拥有的内容(格式化(:

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);
        }
    }
}

正确的缩进是你的朋友。

应将所有方法和属性放在类中 声明。

像这样:

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);
        }
    }
}

在这里的行中:

public class Course
{
    protected string description;
    protected string prefix;
    protected double number;
    protected double hours;
} // Get rid of this line here
public Course ()
{
    ...

类范围内声明您的承包商和方法。

替换您的代码有了这个

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);
        }
    }
}