簡體   English   中英

替換字符串中的char值(Java)

[英]Replacing a char value in a string (Java)

在我的作業中,我需要獲得以下輸出:

輸入一個詞:房子
您要替換什么字母?:e
您想用什么字母代替? w
新詞是housw。

_____________________________________________。

我讓程序可以使用此代碼,但現在需要設置while循環。 這是我當前的代碼。

字串=“”; 掃描儀鍵盤=新的Scanner(System.in);

    System.out.print("Enter a word: " + word);
    word = keyboard.nextLine();

    String readChar = null;
    System.out.print("What letter do you want to replace?: ");
    readChar = keyboard.next();

    String changeChar;
    System.out.print("With what letter do you wish to replace it? ");
    changeChar = keyboard.next();

    keyboard.close();
    System.out.println(word.replaceAll(readChar, changeChar));

    System.out.println();

我現在需要使程序輸出如下:

輸入一個詞:房子
您要替換什么字母?:
沒有房子。
您要替換什么字母?:b
沒有房子。
您要替換什么字母?:e
您想用什么字母代替? w
新詞是housw。


我的while循環如何描繪此輸出?

讀取單詞和要替換的字符(以及要替換為的字符)后,可以使用String類中的replace方法。

這是用法示例(使變量名適應您的代碼)

word = word.replace(letterToReplace, replacementLetter);

所以舉個例子

String word = "aba";
word = word.replace('a', 'c');
System.out.println(word); // Prints out 'cbc'

另外,這里是到JavaDoc的強制鏈接,用於replace方法:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#replace%28char,%20char%29

希望您不要介意進行解釋,下面是可以遵循的示例。

 String a = "HelloBrother How are you!";
 String r = a.replace("HelloBrother","Brother");

 print.i(r);

如果要替換所有字母,可以這樣操作(工作代碼):

public static void main(String[] args) {

    String word = "";
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a word: " + word);
    word = keyboard.nextLine();

    String readChar = null;
    System.out.print("What letter do you want to replace?: ");
    readChar = keyboard.next();

    String changeChar;
    System.out.print("With what letter do you wish to replace it? ");
    changeChar = keyboard.next();

    keyboard.close();
    System.out.println(word.replace(readChar, changeChar));
}

好的,這是實現問題的第二部分的一種可能方法:

public static void main(String[] args) {

    String word = "";
    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a word: " + word);
    word = keyboard.nextLine();

    boolean done = false;
    do{
        String readChar = null;
        System.out.print("What letter do you want to replace?: ");
        readChar = keyboard.next();

        if(word.contains(readChar)){

            String changeChar;
            System.out.print("With what letter do you wish to replace it? ");
            changeChar = keyboard.next();

            done = true;
            keyboard.close();
            System.out.println(word.replace(readChar, changeChar));
        }
    }
    while(!done);
}

這說明了您需要執行的操作。

import java.util.Scanner;
public class WordScrambler{

    public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Enter a word: ");
    String word = keyboard.nextLine();

    System.out.print("What letter do you want to replace?: ");
    char letter = keyboard.next().charAt(0);

    StringBuffer out = new StringBuffer(word);
    System.out.print("With what letter do you wish to replace it? ");
    char changeChar = keyboard.next().charAt(0);

    for (int i=0; i<word.length(); ++i) 
        if(word.charAt(i) == changeChar)
            out.setCharAt(i, changeChar);

    System.out.println("The new word is "+out);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM