使用 switch 语句将字符串中的每个元音替换为以下元音

我是编程和学习 Java 的新手。我正在努力让这个 switch 语句与替换字符串中的元音然后打印新字符串一起工作。我一直在用 ChangeV 找不到符号。另外,不确定如何遍历字符串以检查元音,然后替换每个元音。我想从用户那里得到输入,但又不清楚如何正确合并。谢谢你的帮助!


public class Main{


    public static void main(String args[]){



    //Scanner myScanner = new Scanner(System.in);

    //String s = myScanner.next();

    ChangeV v = new ChangeV();


    System.out.println(v.ChangeV());


}



public void ChangeV(){



String s = "aeiou";

char [] vowels ={'a', 'e' ,'i', 'o','u'};


// I believe I would need a for loop 


switch (//not sure what goes here) {

    case'a' :


    s.replace('a', 'e');


    break;


    case 'e' :


    s.replace('e', 'i');



    break;


    case 'i' :


    s.replace('i', 'o');



    break;


    case 'o' :


    s.replace('o', 'u');


    break;



    case 'u' :


    s.replace('u', 'a');



    break;

}


}

    }


}


蓝山帝景
浏览 101回答 3
3回答

宝慕林4294392

你接近的方式有两个问题。s.replace('a', 'e');不会替换字符串中的任何字符,因为String对象在 Java 中是不可变的。s仅当您将其分配回as时,它才会替换s = s.replace('a', 'e');以这种方式替换循环中字符串的每个字符的问题是,在每次下一次迭代中,replace如果匹配该字符,它甚至会替换任何过去的字符。所以本质上,“aeiou”最后会变成“aaaaa”,因为在最后一个字符处,你会s = s.replace('u', 'a');用 . 替换每个字符a。因此,要替换字符串中的字符,下面是一种更好的方法:public static void main(String[] args){&nbsp; &nbsp; String oldString = "aeiou";&nbsp; &nbsp; char[] characters = oldString.toCharArray();&nbsp; &nbsp; for (int i = 0; i < characters.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; switch (characters[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'a':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'e';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'e':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'i';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'i':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'o';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'o':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'u';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'u':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'a';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; String newString = String.valueOf(characters);&nbsp; &nbsp; System.out.println(oldString);&nbsp; &nbsp; System.out.println(newString);}如果您想用用户输入的任何字符串替换元音,您可以使用Scanner通过控制台读取用户的输入,它可以实现如下。public class Main {&nbsp; &nbsp; public static void main(String[] args){&nbsp; &nbsp; &nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; String oldString = scanner.next();&nbsp; &nbsp; &nbsp; &nbsp; String newString = changeVowels(oldString);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(newString);&nbsp; &nbsp; &nbsp; &nbsp; scanner.close();&nbsp; &nbsp; }&nbsp; &nbsp; private static String changeVowels(String oldString) {&nbsp; &nbsp; &nbsp; &nbsp; char[] characters = oldString.toCharArray();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < characters.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (characters[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'a':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'e';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'e':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'i';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'i':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'o';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'o':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'u';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case 'u':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters[i] = 'a';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return String.valueOf(characters);&nbsp; &nbsp; }}

慕田峪7331174

你写了这一行:ChangeV v = new ChangeV();您是在告诉编译器有一个以ChangeV无参数构造函数命名的类。我在您的代码中没有看到这样的类,只有public class Main.你还写了这一行:public void ChangeV(){这是不正确的,因为它超出了Main类中的大括号。Java 是一种面向对象的语言。每个函数都必须出现在类定义中。像 Python 这样的语言是不同的:您可以在源文件中自己定义一个函数。在 Java 中并非如此。如果它在大括号内,Main它将是Main名为的类上的一个方法ChangeV,它不接受任何参数并返回void。那不是构造函数。构造函数是一种特殊的方法,它与它所属的类同名,并且没有返回类型。这是一个没有参数的构造函数:public ChangeV() {&nbsp; &nbsp; // initialize the instance in here.}我不认为你想要那个。这就是我认为你想要的:public class VowelChanger {&nbsp; &nbsp; public static void main(String [] args) {&nbsp; &nbsp; &nbsp; &nbsp; for (String arg : args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(String.format("before: %s after: %s", arg, changeVowels(args));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static String changeVowels(String s) {&nbsp; &nbsp; &nbsp; &nbsp; // put your code to replace vowels here.&nbsp; &nbsp; }}

慕尼黑的夜晚无繁华

我相信duffymo 的回答已经掩盖了您的方法使用问题,所以我将简单地提及使用replace问题s = s.replace('a', 'e');String.replace将替换每一次出现,所以你可以简单地连续调用它 5 次:s = s.replace( 'a', 'e' );s = s.replace( 'e', 'i' );s = s.replace( 'i', 'o' );s = s.replace( 'o', 'u' );s = s.replace( 'u', 'a' );但是你最终只会得到'a',因为每个'a'都会变成'e',然后每个'e''i'等等,直到每个'u'都变成'a'。好的,我们可以在 where modify 之后反转顺序以查找元音:s = s.replace( 'u', 'a' );s = s.replace( 'o', 'u' );s = s.replace( 'i', 'o' );s = s.replace( 'e', 'i' );s = s.replace( 'a', 'e' );但是我们仍然对以“e”(暂时是“a”)结尾的“u”有问题。因此,一种解决方法是使用临时字符,但如何确保它尚未在您的文本中使用:s = s.replace( 'u', '*' ); //don't set it to 'a' directlys = s.replace( 'o', 'u' );s = s.replace( 'i', 'o' );s = s.replace( 'e', 'i' );s = s.replace( 'a', 'e' );s = s.replace( '*', 'a' ); //then replace the temp character by 'a'更好的解决方案是处理文本,一次处理一个字符。如果你找到一个元音,你就改变它。我的解决方案将使用数组来减少冗长,并让索引为您提供下一个元音。(% array.length如果您在最后一个元音之后,则使用 a 从开头返回)static char [] vowels ={'a', 'e' ,'i', 'o','u'};public static String changeV(String s){&nbsp; &nbsp; char[] chars = s.toCharArray(); //transform a String in an array of character&nbsp; &nbsp; for ( int i = 0; i < chars.length; ++i ) { //iterate the array&nbsp; &nbsp; &nbsp; &nbsp; int index = Arrays.binarySearch( vowels, chars[i] ); //look for the current character in the vowels array and get his index&nbsp; &nbsp; &nbsp; &nbsp; if ( index > -1 ) // is it a vowels&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chars[i] = vowels[( index + 1 ) % vowels.length]; // change with the next vowels&nbsp; &nbsp; }&nbsp; &nbsp; return new String(chars); //create a nez String with the result}这会给你:changeV( "aeiou" ); //eiouachangeV( "This is a test" ); //Thos os e tistchangeV( "Hello world" ); //Hillu wurld
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java