简体   繁体   English

如果满足特定条件,如何更改 Java 中字符串中的元音?

[英]How do I change vowels in a string in Java if a certain condition is met?

How do I make it so that if the 3rd letter of the 1st string I input is found on the 2nd string, it will print the first string in all caps and change its vowels to *?我如何做到这一点,如果我输入的第一个字符串的第三个字母在第二个字符串上找到,它将打印第一个字符串全部大写并将其元音更改为 *? If the 3rd letter is not found on the second string it will print the second string in small letters and replace all the vowels to @?如果在第二个字符串上找不到第三个字母,它将以小写字母打印第二个字符串并将所有元音替换为@?

import java.io.*;
public class Program2 {
    
    public static void main(String args[]) throws IOException {
        BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the first String:");
        String first = data.readLine();
        System.out.println("Enter the second String:");
        String second = data.readLine();
        
        char a[] = first.toCharArray();
        char b[] = second.toCharArray();
        char c = first.charAt(2);
        char ch;
        String text;
        String mute;
        
        for (int w = 0; w < b.length; w++)
        {
            ch = second.charAt(w);
            if (ch == c) {                
                for (int i = 0; i < a.length; i++) {
                    if (a[i]=='a'||a[i]=='e'||a[i]=='i'||a[i]=='o'||a[i]=='u') {
                        a[i]='*'; //replaces the vowels with *
                        }            
                    text = Character.toString(a[i]);
                    System.out.print(text.toUpperCase());
            }
            }
            else if (ch != c) {
                for (int x = 0; x < b.length; x++) {
                    if (b[x]=='a'||b[x]=='e'||b[x]=='i'||b[x]=='o'||b[x]=='u') {
                        b[x]='@'; //replaces the vowels with @
                        }
                    mute = Character.toString(b[x]);
                    System.out.print(mute.toLowerCase());
                }
            }
        }
    }
}public static void main(String args[]) throws IOException {
        BufferedReader data = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the first String:");
        String first = data.readLine();
        System.out.println("Enter the second String:");
        String second = data.readLine();
        
        char b[] = second.toCharArray();
        char c = first.charAt(2);
        char ch;
        String text;
        String mute;
        
        int x = 1;
        while (x < b.length) {
        {
            ch = second.charAt(x);
            if (ch == c) {                
                text = first.toUpperCase();
                System.out.println(text.replaceAll("[AaEeIiOoUu]", "*"));    
            }
            else if (ch != c) {
                mute = second.toLowerCase();
                System.out.println(mute.replaceAll("[AaEeIiOoUu]", "@"));
            }
        x++;    
    }
    }
}
}

somehow the code print multiple copies of the output and sometimes the second string appears even the second condition is not met不知何故,代码打印了 output 的多个副本,有时即使不满足第二个条件,也会出现第二个字符串

Your code prints out one string at each character of the second string, since it runs INSIDE the loop.您的代码在第二个字符串的每个字符处打印一个字符串,因为它在循环内部运行。 So所以

  • get rid of the loop, String.indexOf(char) does what you want摆脱循环, String.indexOf(char) 做你想做的事
  • do the string replacement outside the check to see if a character matches.在检查之外进行字符串替换以查看字符是否匹配。

Something like:就像是:

...
if (second.indexOf(c)>-1){
   ... code for case when second string contains character
} else {
   ... code for case when second string does NOT contains character (indexOf returns -1)
}

Then, instead of using character arrays, investigate the String.replace function to simply replace some characters by others.然后,不要使用字符 arrays,而是研究 String.replace function 以简单地将某些字符替换为其他字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:如何在满足特定条件时排队要执行的异步调用? - Java: How can I queue up asynchronous calls to be executed when a certain condition is met? 满足特定条件时如何更改 android 中的启动器活动? - How to change launcher activity in android when a certain condition is met? 如果满足特定条件,如何测试新活动的UI? - How do I test the UI of a new Activity only if certain condition is met? 满足特定条件时添加消息(Java) - Adding message when certain condition is met (Java) 如何在字符串中找到元音,并在屏幕上打印出具有最多元音的单词? - How do I find vowels in a string, and print the word with the most vowels on the screen? 如果不满足特定条件,我如何终止 Java 程序? - How do I terminate a Java program if a certian condition isn't met? Android Java Runnable,当满足Runnable中的条件时,如何打开另一个活动? - Android Java Runnable, How do I open another activity when a condition in the runnable is met? 如何将以字符串形式编写的条件转换为Java“ if”条件 - How do I convert a condition written in string to a Java “if” condition 如何停止程序直到满足某个条件? - How to stop the program until a certain condition is met? Java:如何更改字符串的颜色? - Java: how do I change the color of a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM