问题描述
我正在C#中调试以下内容,找不到原因我的错误.我很困惑,不知道如何解决它们.任何帮助都是最好的.
//创建一个HomeworkAssignment类
//实例化两个对象
//并提示用户有关两门课程的信息
使用系统;
类DebugNine1
{
static void Main()
{
HomeworkAssignment课程1 =新的HomeworkAssignment();
HomeworkAssignment课程2 =新的HomeworkAssignment();
字符串entryString;
内部练习;
//获取头等舱信息
Console.Write("您要上什么课?");
entry = Console.ReadLine();
course1.className = entryString;
Console.Write(您必须完成多少个练习?");
entryString = Console.ReadLine();
int.TryParse(entryString,练习);
练习= course1.NumberOfExercises;
//获取其他课程的信息
Console.Write("您要上什么课?");
entryString = Console.ReadLine();
course2.className =入口字符串;
Console.Write(您必须完成多少练习?");
entryString = Console.ReadLine();
char.TryParse(entryString,练习);
course2.NumberOfExercises =练习;
Console.WriteLine(您有{0}分钟的时间来做{1}分钟",
course1.timeToComplete,course1.ClassName);
Console.WriteLine(和{0}再花{0}分钟,
course2.timeToComplete,course2.ClassName);
}
}
家庭作业分配
{
private int numberOfExercises;
private int timeToComplete;
//10分钟完成每次练习
private const int TIME_PER_EXERCISE = 10;
public ClassName {get; set};
public int NumberOfExercises
{
得到
{
返回numberOfexercises;
}
设置
{
numberOfExercises =数字;
CalcCompletionTime();
}
}
public double TimeToComplete
{
得到
{
return timeToComplete;
}
}
private void CalcCompletionTime()
{
timeToComplete = numberOfExercises * TIME_PER_EXERCISE;
}
}
// Creates a HomeworkAssignment class
// instantiates two objects
// and prompts the user for infromation about two courses
using System;
class DebugNine1
{
static void Main()
{
HomeworkAssignment course1 = new HomeworkAssignment();
HomeworkAssignment course2 = new HomeworkAssignment();
string entryString;
int exercises;
// Get info for first class
Console.Write("What class do you have homework for? ");
entry = Console.ReadLine();
course1.className = entryString;
Console.Write("How many exercises must you complete? ");
entryString = Console.ReadLine();
int.TryParse(entryString, exercises);
exercises = course1.NumberOfExercises;
// Get info for another class
Console.Write("What class do you have homework for? ");
entryString = Console.ReadLine();
course2.className = entrystring;
Console.Write("How many exercises must you complete? ");
entryString = Console.ReadLine();
char.TryParse(entryString, exercises);
course2.NumberOfExercises = exercises;
Console.WriteLine("You have {0} minutes of homework for {1}",
course1.timeToComplete, course1.ClassName);
Console.WriteLine("and {0} more minutes for {1}",
course2.timeToComplete,course2.ClassName);
}
}
class HomeworkAssignment
{
private int numberOfExercises;
private int timeToComplete;
// 10 minutes to complete each exercise
private const int TIME_PER_EXERCISE = 10;
public ClassName {get; set};
public int NumberOfExercises
{
get
{
return numberOfexercises;
}
set
{
numberOfExercises = number;
CalcCompletionTime();
}
}
public double TimeToComplete
{
get
{
return timeToComplete;
}
}
private void CalcCompletionTime()
{
timeToComplete = numberOfExercises * TIME_PER_EXERCISE;
}
}
以下是错误:
- 预期的类,委托,枚举,接口或结构(以下内容:public int NumberOfExercises,public double TimeToComplete,private void CalcCompletionTime)
- get中的无效令牌;设置行(公共类名{get; set})
- 名称空间定义的类型,或预期的文件结尾(关闭程序的最后一个大括号)
推荐答案
假设您直接从Visual Studio复制并粘贴了此代码,则会有很多语法错误.但是其中两个将涉及到HomeworkAssignment类的ClassName属性.
Assuming that you copied and pasted this code directly from Visual Studio, there are quite a few syntax errors. But two of the ones that will help involve the ClassName property of the HomeworkAssignment class.
该属性没有返回类型,并且分号位于错误的位置.它应该看起来像这样:
The property has no return type, and the semicolon is in the wrong spot. It should look something like this:
public string ClassName {get; set;}
请注意,分号在集合后的位置如何,并且我已将'string'添加为属性的返回值.
Notice how the semicolon is right after the set, and that I've added 'string' as the property return value.
在许多地方,各种属性的大小写不一致. C#区分大小写,因此当您有一个名为"numberOfExercises"的成员时,必须使用相同的拼写.我专门讲NumberOfExercises 属性,您将在其中返回"numberOfexercises".
There are also many places where the casing of various properties is not consistent. C# is case-sensitive, so when you have a member named 'numberOfExercises', you must use the same spelling. I'm speaking specifically of the NumberOfExercises property of HomeworkAssignment, where you return 'numberOfexercises'.
希望这可以帮助您入门!
Hope this gets you started!
菲尔
这篇关于C#调试帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!