2017-10-13 60 views
1

我想只刪除「brealdeke」中的'a' 這個程序的工作原理是,它打印「breldeke」,但如果我把 「brealdeake」與2'a'字符串,它狂暴和打印:breldeakebrealdeke如何解決它?謝謝 我真的很想看看這個:停止在第一次出現的字符

class Example { 
    public static String suppression(char c, String s) { 
     for (int i = 0; i < s.length(); i++) { 
      if (s.charAt(i) == c) { 
       int position = i; 
       for (int a = 0; a < position; a++) { 
        System.out.print(s.charAt(a)); 
       } 
       for (int b = position + 1; b < s.length(); b++) { 
        System.out.print(s.charAt(b)); 
       } 
      } 

     } 
     return ""; 
    } 

    public static void main(String[] args) { 
     // prints "breldeke" 
     System.out.println(suppression('a', "brealdeke")); 
     // prints "breldeakebrealdeke" 
     System.out.print(suppression('a', "brealdeake")); 
    } 
} 
+0

所以,你要刪除的第一個字符的首次出現是不斷重複? –

+0

嗯不是「重複」,我不知道如何解釋它 我是選擇必須刪除的字母的人 以下是一些示例 「軟件」中的'a' - >打印Softwre在「軟件」中打印軟件 或「o」 - >在「番茄」中打印軟件 或'o' - >打印Tmato – Blebhebhe

回答

0

附加變量好嗎?

我會做這樣的個人:

public static String removeFirstLetter(string s, char c) 
{ 
    String word = ""; 
    Bool foundChar = false; 

    for (int i = 0; i<s.length();i++) { 
     if (s.charAt(i).toLower() != c) 
     { 
     word += s.char(i); 
     } 
    else 
    { 
     if (foundChar == false){ 
      foundChar = true; 
     } 
     else 
     { 
      word += s.char(i); 
     } 
    } 
    } 
} 

System.out.print(word); 
+0

HI也非常感謝您的回答, 但由於我們還沒有被教過「toLower 「在學校他們不會允許我們使用:s到目前爲止,我們知道:charAt(),.length(),stringEquals – Blebhebhe

+0

@Blebhebhe如果案件不是問題,你可以不要考慮 –

+0

謝謝感謝,我們知道「布爾」,但我的作業是關於使用一個功能,你可以再看看我的文章,有人編輯它,使其看起來不那麼凌亂 – Blebhebhe

2

你可以嘗試:

"banana".replaceFirst("a", ""); 

這將返回bnana

編輯:但願,這並不包括任何你還沒有被教過

public static void main(String[] args) { 
    String word = "banana"; 
    String strippedWord = ""; 
    boolean found = false; 
    for (int i = 0; i < word.length(); i++) { 
     if (word.charAt(i) == 'a' && !found) found = !found; 
     else strippedWord += word.charAt(i); 
    } 
    System.out.println(strippedWord); 
} 

這將打印bnana

EDIT2:你說你在一個函數想它,同樣適用:

public static String suppression(char c, String word) { 
    String strippedWord = ""; 
    boolean charRemoved = false; // This is a boolean variable used to know when the char was skipped! 
    for (int i = 0; i < word.length(); i++) { 
     // If the current letter is for example 'a' and we haven't yet skipped the char, skip this char we're at 
     if (word.charAt(i) == c && charRemoved == false) charRemoved = true; 
     else strippedWord += word.charAt(i); 
    } 
    return strippedWord; 
} 

public static void main(String[] args) { 
    // prints "breldeke" 
    System.out.println(suppression('a', "brealdeke")); 
    // prints "breldeake" 
    System.out.print(suppression('a', "brealdeake")); 
} 
+0

我總是忘記Java擁有所有這些幾乎不切實際的操作。 –

+0

嗨,謝謝你的回答,但由於我們還沒有被教導 「.replace」在學校他們不會允許我們使用:s 到目前爲止,我們知道:charAt(),.length() ,stringEquals – Blebhebhe

+0

謝謝,我真的非常感謝你爲我做了很多但是從未見過的發現:s 我編輯了我的帖子,試圖讓它不那麼凌亂,如果你可以再看看它,謝謝 – Blebhebhe

相關問題