Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将第n个(来自用户的输入)数字转换为大写,其余数字将使用小写_Java_String_Ms Word_Uppercase_Lowercase - Fatal编程技术网

Java 将第n个(来自用户的输入)数字转换为大写,其余数字将使用小写

Java 将第n个(来自用户的输入)数字转换为大写,其余数字将使用小写,java,string,ms-word,uppercase,lowercase,Java,String,Ms Word,Uppercase,Lowercase,我再问一次。我有一个问题,就是创建一个程序来读取用户输入的字符串(句子或单词)。第n个数字(来自用户)将变为大写,其余将变为小写。 例如: string=“大家早上好” n=2 输出=大家早上好 for (int x = 0; x < s.length(); x++) if (x == n-1){ temp+=(""+s.charAt(x)).toUpperCase(); }else{ temp+=("

我再问一次。我有一个问题,就是创建一个程序来读取用户输入的字符串(句子或单词)。第n个数字(来自用户)将变为大写,其余将变为小写。 例如:

string=“大家早上好”

n=2

输出=大家早上好

    for (int x = 0; x < s.length(); x++)
        if (x == n-1){
            temp+=(""+s.charAt(x)).toUpperCase();
        }else{
            temp+=(""+s.charAt(x)).toLowerCase();
        }
    s=temp;
    System.out.println(s);
}
for(int x=0;x

输出:大家早上好

我知道你们想要发生什么-但你们的问题表达得不太好。你唯一缺少的部分就是重复句子中的每个单词。如果你问“如何对字符串中的每个单词应用函数”,你可能会得到更好的回答

这有点草率,因为它在末尾添加了一个尾随的“”,但是您可以很容易地修复它

public class Test {

    static String test = "This is a test.";

    public static void main(String[] args) {
        String[] words = test.split(" ");
        String result = "";

        for (String word : words) {
            result += nthToUpperCase(word, 2);
            result += " ";
        }

        System.out.println(result);
    }

    public static String NthToUpperCase(String s, int n) {
        String temp = "";

        for (int i = 0; i < s.length(); i++) {
            if (i == (n-1)) {
                temp+=Character.toString(s.charAt(i)).toUpperCase();
            } else {
                temp+=Character.toString(s.charAt(i));
            }
        }

        return temp;
    }
}
公共类测试{
静态字符串测试=“这是一个测试。”;
公共静态void main(字符串[]args){
String[]words=test.split(“”);
字符串结果=”;
for(字符串字:字){
结果+=第n个病例(字,2);
结果+=”;
}
系统输出打印项次(结果);
}
公共静态字符串NthToUpperCase(字符串s,int n){
字符串temp=“”;
对于(int i=0;i
您可以使用两个for循环来完成此操作。迭代每个单词,并在迭代中迭代每个字符

toUpperCase(2, "good morning everyone");

private static void toUpperCase(int nth, String sentence) {
    StringBuilder result = new StringBuilder();
    for(String word : sentence.split(" ")) {
        for(int i = 0; i < word.length(); i++) {
            if(i > 0 && i % nth - 1 == 0) {
                result.append(Character.toString(word.charAt(i)).toUpperCase());
            } else {
                result.append(word.charAt(i));
            }
        }
        result.append(" ");
    }
    System.out.println(result);
}
toUpperCase(2,“大家早上好”);
私有静态void toUpperCase(int n,字符串语句){
StringBuilder结果=新建StringBuilder();
for(字符串字:句子。拆分(“”){
for(int i=0;i0&&i%n-1==0){
result.append(Character.toString(word.charAt(i)).toUpperCase());
}否则{
结果.追加(word.charAt(i));
}
}
结果。追加(“”);
}
系统输出打印项次(结果);
}
大家早上好


那么-有什么问题?你的问题是什么?你有一个基于单词的函数。因此,遍历这些单词并将函数应用于每个单词。您从未在问题的任何地方编写过希望
n
输入的内容。因为每个单词都是大写的,所以只能在
输出
行中看到。你也没有问过一个问题,这能回答你的问题吗?或者我想我在这个句子的第一个单词中得到了它,但我不知道下一个单词该怎么做,因为它之间有空格