Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换字符串中的字符值(Java)_Java_Arrays_Char - Fatal编程技术网

替换字符串中的字符值(Java)

替换字符串中的字符值(Java),java,arrays,char,Java,Arrays,Char,在我的作业中,我需要获得以下输出: 输入一个单词:house 您想替换哪个字母?:e 你想用什么字母来代替它?w 新词是housw _____________________________________________ 我让程序使用这段代码,但现在我需要设置while循环。这是我目前的代码 字串=”; 扫描仪键盘=新扫描仪(System.in) 我现在需要使我的程序输出如下: 输入一个单词:house 您要替换哪个字母?:a 房子里没有房子。 你想替换哪个字母?:b 房子里没有房子。 您想

在我的作业中,我需要获得以下输出:

输入一个单词:house
您想替换哪个字母?:e
你想用什么字母来代替它?w
新词是housw

_____________________________________________

我让程序使用这段代码,但现在我需要设置while循环。这是我目前的代码

字串=”; 扫描仪键盘=新扫描仪(System.in)


我现在需要使我的程序输出如下:

输入一个单词:house
您要替换哪个字母?:a
房子里没有房子。
你想替换哪个字母?:b
房子里没有房子。
您想替换哪个字母?:e
你想用什么字母来代替它?w
新词是housw



我的while循环将如何描述此输出?

我希望您不介意硬解释,下面是您可以遵循的示例

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

 print.i(r);

我希望你不介意硬解释,下面是你可以遵循同样的例子

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

 print.i(r);

阅读单词和要替换的字符(加上要替换的字符)后,可以使用String类中的replace方法

下面是一个示例用法(根据代码调整变量名)

比如说

String word = "aba";
word = word.replace('a', 'c');
System.out.println(word); // Prints out 'cbc'
这里还有一个指向
replace
方法的JavaDoc的强制性链接:


阅读单词和要替换的字符(加上要替换的字符)后,可以使用String类中的replace方法

下面是一个示例用法(根据代码调整变量名)

比如说

String word = "aba";
word = word.replace('a', 'c');
System.out.println(word); // Prints out 'cbc'
这里还有一个指向
replace
方法的JavaDoc的强制性链接:


如果要替换所有字母,可以这样做(工作代码):


如果要替换所有字母,可以这样做(工作代码):


这说明了您需要做什么

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);
    }
}
import java.util.Scanner;
公共类字扰频器{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入单词:”);
字符串字=键盘.nextLine();
System.out.print(“要替换的字母:”;
字符字母=键盘.next().charAt(0);
StringBuffer out=新的StringBuffer(字);
System.out.print(“您希望用什么字母替换它?”);
char changeChar=keyboard.next().charAt(0);

对于(int i=0;i这说明了您需要做什么

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);
    }
}
import java.util.Scanner;
公共类字扰频器{
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入单词:”);
字符串字=键盘.nextLine();
System.out.print(“要替换的字母:”;
字符字母=键盘.next().charAt(0);
StringBuffer out=新的StringBuffer(字);
System.out.print(“您希望用什么字母替换它?”);
char changeChar=keyboard.next().charAt(0);

对于(int i=0;i好的)问题,这是实现问题第二部分编辑的一种可能方法:

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);
}

好的,这是实施问题第二部分的一种可能方式:

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);
}

String有一个名为replace的方法,该方法使用char sequenceString有一个名为replace的方法,该方法使用char sequenceStringsequence@Owlex我想你应该试试上面Andreas的代码。你能检查我的问题编辑吗?我用了你的代码,现在我有一个关于下一部分的问题。谢谢你到目前为止的帮助。之前我在考虑设置正在设置空值,但在看到您的答案之前不确定。@LokeshDo如果字符串中没有“a”,是否要显示消息?我的意思是将上面的注释转到@AndreasFirst step-使用增强循环来循环读取字符。第二步-检查它是否不包含表示为(!somename.contains(“a”)的“a”)。您可以为流程的其余部分添加else语句。@Owlex我认为您应该尝试上面Andreas编写的代码。您可以检查我的问题编辑吗?我使用了您的代码,现在我有一个关于下一部分的问题。感谢您迄今为止的帮助。以前我考虑过设置null,但在看到您的答案之前不确定。@LokeshDo y如果字符串中没有“a”,是否要显示消息?我的意思是上面的注释转到@AndreasFirst step-使用增强循环来循环读取字符。第二步-检查它是否不包含表示为(!somename.contains(“a”)的“a”)。您可以为流程的其余部分添加else语句。整个
是否要替换为哪个字母?
不需要在循环中整个
是否要替换为哪个字母?
不需要在循环中