将单词nice替换为大写的nice java

将单词nice替换为大写的nice java,java,Java,如果我的问题很难理解,我很抱歉,英语不是我的主要语言,希望你们能容忍我的英语。我正在编写一个代码,读取用户输入的文本,并将某些单词转换为所有大写字母,例如nice变成nice。nice这个词可以是小写和大写的组合,比如nice等等,最后这些“nice”会被转换成nice。由于有许多“nice”字的组合,我将初始输入文本设置为所有小写,并用nice替换“nice”。我现在的问题是,我不知道如何打印出最终结果,这些结果都是由nice word转换成nice的。我现在正在写的程序正在逐1进行转换。如果

如果我的问题很难理解,我很抱歉,英语不是我的主要语言,希望你们能容忍我的英语。我正在编写一个代码,读取用户输入的文本,并将某些单词转换为所有大写字母,例如nice变成nice。nice这个词可以是小写和大写的组合,比如nice等等,最后这些“nice”会被转换成nice。由于有许多“nice”字的组合,我将初始输入文本设置为所有小写,并用nice替换“nice”。我现在的问题是,我不知道如何打印出最终结果,这些结果都是由nice word转换成nice的。我现在正在写的程序正在逐1进行转换。如果我的字符串输入中有很多“漂亮”的单词,那么这个词就很长了。有更好的方法吗?非常感谢你。这是我的密码

import java.util.Scanner;

public class Substitute
{
    public static void main (String[] args)
    {

        String search = "nice";

        String sub = "NICE";

        String result = "";

        int i;

        Scanner input = new Scanner(System.in);

        String yourSentence;

        System.out.print("enter your text here: ");

        yourSentence = input.nextLine();

        String actualWord = yourSentence.toLowerCase();


        do 
        {

            System.out.println(actualWord);

            i = actualWord.indexOf(search);

            if (i != -1)
            {

                result = actualWord.substring(0,i);

                result = result + sub + actualWord.substring(i + search.length());

                actualWord = result;
            }

        } while (i != -1);


    }
}
这是

my output --------------------Configuration: <Default>--------------------
enter your text here: be nice to your families and be NicE to your friends too. Also be NICe to everyone in this world

be nice to your families and be nice to your friends too. also be nice to everyone in this world

be NICE to your families and be nice to your friends too. also be nice to everyone in this world

be NICE to your families and be NICE to your friends too. also be nice to everyone in this world

be NICE to your families and be NICE to your friends too. also be NICE to everyone in this world

Written by blabla


Process completed.
我的输出------------------配置:--------------------
在这里输入你的文字:善待你的家人,也善待你的朋友。也要善待这个世界上的每一个人
善待你的家人,也善待你的朋友。也要善待这个世界上的每一个人
善待你的家人,也善待你的朋友。也要善待这个世界上的每一个人
善待你的家人,也善待你的朋友。也要善待这个世界上的每一个人
善待你的家人,也善待你的朋友。也要善待这个世界上的每一个人
布拉布拉写的
进程已完成。

你的句子怎么样

您可以使用正则表达式,当您想要替换多个对象时,它特别有用,这里有一个关于好、坏和丑的示例:)


这应该能解决你的问题

Scanner input = new Scanner(System.in);
//Convert the Scanner object to a String
String str = input.toString();
//Get the beginning index for "nice"
int begin = str.indexOf("nice");
//Get the ending index for "nice"
int end =  str.indexOf(" ", begin );
//Replace the string between begin and end with NICE
str.replace("nice", "NICE");

我不知道如何只打印出最终结果
移动行
System.out.println(实际值)
do/while
循环下面。@Tom。这就像我想要的那样。非常感谢你,汤姆。不过我有个问题,可以把System.out.println(actualWord)移动吗;在do/while循环之下?我认为对于do/while循环,我总是必须在“do”循环的开始处编写语句,然后在“while”条件之后编写语句。循环体(介于
{
}
之间的代码)可以包含应该执行
x
次的所有内容(或者只要某个条件
为true
)。如果要多次打印某些内容,请将其写入循环体。如果您不想这样做,那么就不要在其中写入:D.没有规则规定,循环需要一个
System.out.println
。我明白了,我只是按照java网站和教科书中的示例编写代码。非常感谢您的解释。我们可以做一笔交易:如果您能告诉我,为什么
将扫描对象转换为字符串
不是胡说八道,我将删除下行投票。如果您解释
开始
结束
的好处,我甚至会添加上行投票。)
Scanner input = new Scanner(System.in);
//Convert the Scanner object to a String
String str = input.toString();
//Get the beginning index for "nice"
int begin = str.indexOf("nice");
//Get the ending index for "nice"
int end =  str.indexOf(" ", begin );
//Replace the string between begin and end with NICE
str.replace("nice", "NICE");