如何替换任何句子中的单词 [英] How to replace words in any sentence

查看:85
本文介绍了如何替换任何句子中的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我必须创建一个控制台应用程序,让用户输入任何句子,然后被问到是否要替换该句子中的单词,例如外星人非常真实,看起来很可怕。询问他想要替换哪个词,如非常和不。然后询问用户是否/无问题是否要替换另一个单词。如果是,那么如果没有显示外星人不真实且看起来很可怕,则将scary这个词替换为funny。继续询问用户是否要更换更多单词,直到用户说不。



我的代码到目前为止看起来像这样......我需要帮助/没有问题然后再次提示用户提出问题并在替换句子中使用...请帮助我



Hi guys, I must create a console application that lets a user enter any sentence and then be asked if he wants to replace a word in that sentence for example "Aliens are very real and looks scary." Ask which word does he want to replace like "very" with "not". Then ask user with yes/no question if he wants to replace another word. If yes then replace word like "scary" with "funny" if no display "Aliens are not real and looks scary." Keep on asking user if he wants to replace more words until user says "no".

My code looks like this so far...I need help with the yes/no question and then prompting user with the question again and using that in the replaced sentence ...please help me

static void Main(string[] args)
        {
            //Enter sentence
            Console.WriteLine("Enter your sentence : \n\n");
            string sSentence = Console.ReadLine();
            Console.WriteLine("\n");


            //Words to be replaced
            Console.WriteLine("Which word do you want to replace? \n");
            string sFirstWord = Console.ReadLine();
            Console.WriteLine("\n");
            Console.WriteLine("Enter the word you want to replace it with: \n");
            string sFirstWordReplace = Console.ReadLine();
            Console.WriteLine("\n");

            //
            //Not sure what to do here with yes/no
            //and redisplay questions if yes
            //

            //Call yes/no question
            AnotherOne();

            //If yes ask second word to be replaced
            Console.WriteLine("Which word do you want to replace? \n");
            string sSecondWord = Console.ReadLine();
            Console.WriteLine("\n");
            Console.WriteLine("Enter the word you want to replace it with: \n");
            string sSecondWordReplace = Console.ReadLine();
            Console.WriteLine("\n");

            //Replace the words
            string sFirst = sSentence.Replace(sFirstWord, sFirstWordReplace);
            Console.WriteLine(sFirst);

            //Exit the program
            Console.WriteLine("\n");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }

        static bool AnotherOne()
        {
            while (true)
            {
                Console.WriteLine("Do you want to replace another word? \n");
                String r = (Console.ReadLine() ?? "").ToLower();
                if (r == "y")
                    return true;
                if (r == "n")
                    return false;
            }
        }

推荐答案

假设你的代码做了它想做的事情



它看起来像:



assuming that your code do what it suppose to do

It would looks like :

static void Main(string[] args)
 {
    //Enter sentence

     while ( wantToReplace() ){
          //Words to be replaced
          //Replace the words
      }

     //Exit the program
}
 
static bool wantToReplace() {

    Console.WriteLine("Do you want to replace another word? \n");
                String r = (Console.ReadLine() ?? "").ToLower();
                if (r == "y")
                    return true;
                if (r == "n")
                    return false;
            
 }







注意:注释行表示代码部分你有




note : the comment line indicates that code section that you have


你可以使用 String.Replace方法(字符串,字符串) ) [ ^ ]至替换更改字符串数据。它将所有出现的一个子字符串更改为另一个子字符串。
You can use String.Replace Method (String, String)[^] to replace changes string data. It changes all occurrences of one substring into another substring.


这篇关于如何替换任何句子中的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆