亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Java中以特定間隔將字符添加到字符串值

如何在Java中以特定間隔將字符添加到字符串值

收到一只叮咚 2022-05-25 09:49:19
我在下面的代碼中遇到了問題我正在構建一個項目,該項目需要用戶輸入"This is some \"really\" great. (Text)!?",然后將其轉換為THISISSOMEREALLYGREATTEXT并將值傳遞給下一個參數。然后在我的 Obify 方法中,我試圖在每個元音前面添加 OB,AEIOUY但在我的函數中它并沒有有效地做到這一點,它會打印出THISISSOMEREALLYGREATTEXT很多次,并且每次通過THISISSOMEREALLYGREATTEXT它時,它都會在最后添加 OB,當我需要 OB 時在每個元音之前,而不是在末尾。請務必告訴我哪里出錯了,這樣我才能繼續進步。再次提前感謝您,正在審查的代碼如下。import java.util.*;public class Main {public static void  main(String[] args) {    normalizeText();    obify("THISISSOMEREALLYGREATTEXT" );}// Part 1 Normalized Method to convert all letter to uppercase and removing all special characters and placing it into String codey.public static String normalizeText (){    Scanner sc = new Scanner( System.in );    String normText;    System.out.println("Please input text to encrypt");    normText = sc.nextLine();    System.out.println(normText.replaceAll(" ","").replaceAll("[^a-zA-Z ]", "").toUpperCase());    return normText;}//This method will take in the Normalized text and insert a capital O and B in-front of every vowel and return the textprivate static String obify (String input) {    String obifiledInput = input;    for (int i = 0; i < input.length(); i++) {        if (input.contains( Character.toString( input.charAt( i ) ) )) {            obifiledInput = obifiledInput + "OB" + Character.toString( input.charAt( i ) );        } else {            obifiledInput = obifiledInput + Character.toString( input.charAt( i ) );        }            System.out.println(obifiledInput);    }    return obifiledInput;}}
查看完整描述

3 回答

?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

查看您的obify功能,我不太清楚您在哪里檢查是否Character是元音。下面的代碼是什么:


if (input.contains( Character.toString( input.charAt( i ) ) ))

正在做的是檢查是否input在input. 這并不能完全解決您檢查字母是否為元音的問題。此外,您可以直接設置obifiledInput為input,而無需先經過并OB在需要時添加。要解決這些問題,您可以嘗試以下我的代碼:


private static String obify (String input) {

    String obifiledInput = "";

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

        char temp = input.charAt(i);

        if (char == 'A' || char == 'E' || char == 'I' || char == 'O' || char == 'U') {

            obifiledInput += "OB" + temp;

        } else {

            obifiledInput += "" + temp;

        }

    }

    return obifiledInput;

}

我認為您應該會發現這是可行的。它檢查字符是否是元音,["A", "E", "I", "O", "U"]. 然后,它將"OB"和 字符添加到obifiledInput。否則,它只會添加Character. 最后,它返回String已經“obified”的。


查看完整回答
反對 回復 2022-05-25
?
米脂

TA貢獻1836條經驗 獲得超3個贊

只需將 input.charAt(i) 與它應該可以正常工作的所有元音匹配。您的代碼中還有一個小錯誤,您已經使用輸入初始化了 oblifiedInput,然后您將 OB 添加到該字符串。即使您的 for 循環是正確的,它仍然不會產生所需的輸出。


private static String obify (String input) {


   String obifiledInput = "";

   char c;

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

       c=input.charAt(i);

       if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {

           obifiledInput +="OB";

       }

       obifiledInput +=c;

   }

   System.out.println(obifiledInput);

   return obifiledInput;

}


查看完整回答
反對 回復 2022-05-25
?
幕布斯6054654

TA貢獻1876條經驗 獲得超7個贊

使用char數組,將所述數組傳遞給每個結構的 a ,然后replaceAll(String regex, String replacement)在您的 String 上使用該方法不是更容易嗎?


此外,還有將“OB”的“O”視為代碼的另一個元音的風險;所以'O'必須是第一個要檢查的元音。


它會給出以下內容:


String obifiledInput = input;

ArrayList<Character> vowels = { 'O', 'A', 'E', 'I', 'U', 'Y' };


for (char curr_char : vowels) {

    obifiledInput.remplaceAll( "[" + curr_char + "]", "OB" + curr_char)


    System.out.println(obifiledInput);  

}


return obifiledInput;


查看完整回答
反對 回復 2022-05-25
  • 3 回答
  • 0 關注
  • 186 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號