簡體   English   中英

替換字符串句子中的多個單詞C#

[英]Replacing multiple words in a string sentence C#

大家好,我不需要答案,但我想知道並找出我做錯了什么。 作為初學者,我在學習中得到了一項非常“簡單”的作業。 我需要創建一個字符串,在這個字符串中,我需要用其他單詞替換一些單詞而不使用 for 循環:(我也想打印它,但我不知道把 Console.WriteLine 放在哪里,谷歌搜索 1小時沒有工作或問大學。

/* 練習:與 stringbuilder 一起使用 * cat 變成 littlecat * dog 變成 littledog * 鼠標變成 littlemouse * 必須用 a 替換的單詞 * 不要使用循環 */


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 開始?)

錯誤 CS8112:replacethisstring(string)' 是一個本地函數,因此必須始終有一個主體。 (我只是給了它一個主體,對嗎?我打開 { 並關閉它 } 並用 return 替換。)

方法聲明以;結尾; 這就是CS8112的原因

Main 方法必須返回void (或 'int' )您已將其修改為string ,這就是CS5001的原因

如果您希望程序在控制台上打印輸出,請使用:

using System;

....

 Console.WriteLine(output)
  1. 您的 main 應該有一個 void 作為返回類型。 字符串是不允許的,但 int 是一個選項( 見參考
  2. 你不能有; 在具有主體的函數聲明的末尾。
  3. 您必須先聲明一個變量,然后才能使用它... string hondKat;
  4. 請參閱以下代碼中 StringBuilder 的使用,而不是字符串。
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)
    {
    ...
    }

有幾個問題,如拼寫錯誤、語法錯誤等。此外,該練習有一個需要與 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;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM