在一个单词中交换字母(Java)

huangapple go评论59阅读模式
英文:

Swapping letters in a word (Java)

问题

我有这个问题:在一个单词中交换字母(Java)

我尝试通过不同的整数值 i 和 j 来解决这个问题。但我找到的最合适的值是 1 和 5。然而,即使在这种情况下,输出也接近正确的版本,但不完全正确。这是我的代码:

public class test {
    public static void main(String[] args) {

        String str = "Gateway";
        int i = 1, j = 5;
        String first = str.substring(0, i);
        System.out.println(first);
        char second = str.charAt(j);
        System.out.println(second);
        String third = str.substring(i + 1, j - 1);
        System.out.println(third);
        System.out.println(str.charAt(i));
        System.out.println(str.substring(j + 1));
        
    }
}

这会导致输出:G a te a y

我的代码有问题吗,还是我选择了错误的整数值?我一直在努力找出问题,但肯定没有帮助。我希望有人能指出我正在犯的错误。

英文:

I have this question: 在一个单词中交换字母(Java)

I tried solving the question through various integer values of i and j. But the most suitable ones I could find was 1 and 5. However even then the output was near to the correct version and not properly correct. Here's my code:

public class test {
    public static void main(String[] args) {

        String str = "Gateway";
        int i = 1, j =5;
        String first = str.substring(0, i);
        System.out.println(first);
        char second = str.charAt(j);
        System.out.println(second);
        String third = str.substring(i + 1, j -1);
        System.out.println(third);
        System.out.println(str.charAt(i));
        System.out.println(str.substring(j + 1));
        
    }
}

This results in the output: G
a
te
a
y

Is there something wrong with my code or am I taking the wrong integer values? I've been trying to figure out but certainly that has been of no help. I hope somebody can point out the mistake I'm doing.

答案1

得分: 0

Java是一种以零为基础的索引语言。使用这些值:

int i = 1; // 第一个“a”
int j = 3; // 第一个“e”

// 注意:substring()的结束参数是“排除”的 - 请参见下面的错误修复!

为了清晰和便于调试,尝试像这样编写代码:

String first = str.substring(0, i);
char second = str.charAt(j);
String third = str.substring(i + 1, j); // 注意:已修正错误!
char fourth = str.charAt(i);
String fifth = str.substring(j + 1);

System.out.println(first + second + third + fourth + fifth);

查看演示

英文:

Java is a zero-based index language. Use these values:

int i = 1; // the first "a"
int j = 3; // the first "e"

substring()’s end parameter is exclusive - see bug fix below!

Try coding like this for clarity and debug-ability:

String first = str.substring(0, i);
char second = str.charAt(j);
String third = str.substring(i + 1, j); // Note: corrected bug!
char fourth = str.charAt(i);
String fifth = str.substring(j + 1);

System.out.println(first + second + third + fourth + fifth);

See live demo.

答案2

得分: 0

两件事情:

  1. i 和 j 的选择,其中 i = 1,j = 3(基本上是要交换的字母的索引)。
  2. String third = str.substring(i + 1, j - 1);

应改为

String third = str.substring(i + 1, j);

因为 substring 方法的第二个参数是结束索引(不包含在子字符串内),即如果你想要子字符串包括索引 j-1,你需要将参数设置为 j。

英文:

Two things:

  1. The choice of i and j, i = 1, and j = 3 (basically the indices of the letters to be swapped).
  2. String third = str.substring(i + 1, j - 1);

should be

String third = str.substring(i + 1, j);

as substring goes till the index just before the one mentioned in the second argument, i.e if you want the substring to include j-1, you have to set the parameter as j.

huangapple
  • 本文由 发表于 2020年10月11日 23:20:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64305692.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定