大家好,我不需要答案,但我希望知道并找出我做错了什么。作为一个初学者,我在学习中得到了一个非常“轻松”的任务。我需要创建一个字符串,并且在此字符串中,我需要用其他单词替换某些单词,而无需使用for循环:(同样我想打印它,但是我不知道将Console.WriteLine和google放在哪里小时没有工作或问大学。
/ * Excersice:与stringbuilder一起使用 *猫成为小猫 *狗变成小狗 *鼠标变成小老鼠 *必须替换为的单词 *不要使用循环* /
using System;
using System.Collections.Generic;
using System.Text;
namespace Opgavens_leerpad_3_oefening
{
class Program
{
static string Main(string[] args)
{
StringBuilder sb = new StringBuilder();
string dogCat = new string("Can the cat find the mouse without waking the dog.");
static string replacethisstring(string dogCat);
{
hondKat = dogCat.Replace("cat", "littlecat");
hondKat = dogCat.Replace("dog", "littldog");
hondKat = dogCat.Replace("mouse", "littlemouse");
hondKat = dogCat.Replace("the", "a");
return dogCat;
}
}
}
}
错误CS5001:程序不包含适用于入口点的静态“ Main”方法(我不明白这不是几乎所有程序都以该静态Main args开始吗?) < / p>
错误CS8112:replacethisstring(string)'是局部函数,因此必须始终具有主体。 (我刚刚给它一个身体正确吗?我打开了{并关闭了它},然后用return替换了它。)
答案 0 :(得分:3)
方法声明以;
结尾,这就是 CS8112
Main方法必须返回您已将其修改为void
的{{1}}(或'int'),这就是 CS5001
如果您希望程序在控制台上打印输出,请使用:
string
答案 1 :(得分:1)
;
。namespace Opgavens_leerpad_3_oefening
{
class Program
{
public static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
sb.Append("Can the cat find the mouse without waking the dog.");
sb = replacethisstring(sb);
Console.WriteLine(sb.ToString());
Console.ReadLine(); // To Stop the Console from closing.
static StringBuilder replacethisstring(StringBuilder dogCat)
{
dogCat = dogCat.Replace("cat", "littlecat");
dogCat = dogCat.Replace("dog", "littldog");
dogCat = dogCat.Replace("mouse", "littlemouse");
dogCat = dogCat.Replace("the", "a");
return dogCat;
}
}
}
}
您可以将功能放置在Main或外部。通常,您会在Main类之外找到函数。
public static void Main(string[] args)
{
...
}
public static string replacethisstring(string dogCat)
{
...
}
答案 2 :(得分:0)
有多个问题,如拼写错误,语法错误等。 此外,练习具有需要与stringbuilder一起使用的条件。
所以,尝试这个。
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder("Can the cat find the mouse without waking the dog?");
sb = replacethisstring(sb);
Console.WriteLine(sb.ToString());
Console.ReadLine();
}
static StringBuilder replacethisstring(StringBuilder dogCat)
{
StringBuilder hondKat = dogCat.Replace("cat", "littlecat");
hondKat = dogCat.Replace("the", "a");
hondKat = dogCat.Replace("dog", "littledog");
hondKat = dogCat.Replace("mouse", "littlemouse");
return hondKat;
}