【问题标题】:Java Replacing a Character with a CharacterJava用字符替换字符
【发布时间】:2016-03-27 01:17:17
【问题描述】:

我正在自学 Java,并使用在线练习进行练习。到目前为止,我只学到了一些方法,所以在这个练习中使用数组超出了我的范围,尽管网上有几个解决方案使用数组来做我想做的事。

练习是这样的:让用户输入带有元音的字符串。只要有元音字母,就将该元音显示为大写字母。

例子:如果用户输入“apples”,正确的输出是AppleEs

到目前为止我有这个代码:

import java.util.Scanner;

public class CapitalizeVowels {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter a string ~ ");
        String string = keyboard.nextLine();

        for (int i = 0; i < string.length(); i++) {
            System.out.print(string.charAt(i));
            if (string.charAt(i) == 'a' || 
                string.charAt(i) == 'e' || 
                string.charAt(i) == 'i' || 
                string.charAt(i) == 'o' || 
                string.charAt(i) == 'u') {

                char upperCaseVowel = Character.toUpperCase(string.charAt(i));
                System.out.print(upperCaseVowel);

                // need to replace string.charAt(i) with upperCaseVowel
                // find something to replace characters
            }
        }
    }
}

例如,当我使用输入字符串“apples”按原样运行代码时,我得到“aAppleEs”作为输出。正在打印小写元音和大写元音。我在想我应该用 upperCaseVowel 替换 string.charAt(i) ,它是小写元音,但我找不到任何 replace() 方法或对字符有这种效果的东西。我尝试了其他的东西,比如 StringBuilder 等,但我还没有遇到一个足够简单的解决方案来避免数组,因为我还没有学习它们。非常感谢任何有关如何获得正确输出的帮助。谢谢!

【问题讨论】:

标签: java replace character capitalization


【解决方案1】:

你的错误是在测试它是否是元音之前打印每个字符。

相反,在确定每个字符应该是什么后打印它。你的循环体应该是:

char next = string.charAt(i);
if (next == 'a' || 
    next == 'e' || 
    next == 'i' || 
    next == 'o' || 
    next == 'u') {
    next = Character.toUpperCase(next);
}
System.out.print(next);

您可以考虑添加:

else {
    next = Character.toLowerCase(next);
}

强制非元音小写。

【讨论】:

  • 这是另一种很棒的方法,直到经过深思熟虑后我才想到。 :P
【解决方案2】:

您只需将if 之前的Sysout 语句移动到else,以避免重复打印相同的字符,例如:

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a string ~ ");
    String string = keyboard.nextLine();

    for (int i = 0; i < string.length(); i++) {

        if (string.charAt(i) == 'a' || string.charAt(i) == 'e' || string.charAt(i) == 'i' || string.charAt(i) == 'o'
                || string.charAt(i) == 'u') {

            char upperCaseVowel = Character.toUpperCase(string.charAt(i));
            System.out.print(upperCaseVowel);

            // need to replace string.charAt(i) with upperCaseVowel
            // find something to replace characters
        }else{
            System.out.print(string.charAt(i));
        }
    }
}

【讨论】:

  • 啊!我懂了。我正确地完成了算法,但我没有从逻辑上打印出来......谢谢!
【解决方案3】:

试试这个,它对我有用。

class ReplaceVowel {

    public static void main(String[] args) {
        String words = "apples";
        char converted = 0;
        String w = null;
        for (int i = 0; i < words.length(); i++) {
            if (words.charAt(i) =='a'|| words.charAt(i)=='e'||words.charAt(i)=='i'||words.charAt(i)=='o'||words.charAt(i)=='u') {
                converted = Character.toUpperCase(words.charAt(i));
                w = words.replace(words.charAt(i), converted);
                words = w;
                    
            } else {
                converted = Character.toUpperCase(words.charAt(i));
                w = words.replace(words.charAt(i), converted);
                words = w;
            }
        }
        System.out.println(words);
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 2013-07-09
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多