【问题标题】:How to change a String as I am scanning text Java [duplicate]当我扫描文本Java时如何更改字符串[重复]
【发布时间】:2016-03-14 22:55:43
【问题描述】:

我正在尝试替换“。”带有“X”。这最终适用于计算机将计算出的基于文本的迷宫。我只想改变第一个,希望我能从那里解决剩下的问题。这是我目前所拥有的。

Scanner sc = new Scanner(maze2);

public void traverseMaze(int row, int col, int handLocationX, int handLocationY) {

    for(int rowCount = 0; rowCount <=11 ; rowCount ++){
        row = rowCount;

        for(int colCount = 0; colCount <= 11; colCount++){
            col = colCount;

            if(row ==2 && col ==0){
                System.out.println(col);
                System.out.println(row);
                sc.next().replace(".", "X"); //stuck here HELP! :)
            }
        }
        System.out.println(maze2);
    }

【问题讨论】:

  • 什么是sc?请解释你做了什么,有没有错误。
  • 抱歉,sc 是扫描仪。没有错误。
  • 如果 sc.next() 返回一个字符串,请注意 String.replace() 不会修改字符串本身,而是返回一个已替换出现的新字符串。跨度>
  • 我会试一试,谢谢。

标签: java maze scanning


【解决方案1】:

正如 cmets 中提到的,String 是不可变的,所以

sc.next().replace(".", "X"); //stuck here HELP! :)

应该是这样的

String orgStr = sc.next();
String newStr = orgStr.replace(".", "X");

【讨论】:

    猜你喜欢
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多