英文:
Swapping letters in a word (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:
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
两件事情:
- i 和 j 的选择,其中 i = 1,j = 3(基本上是要交换的字母的索引)。
String third = str.substring(i + 1, j - 1);
应改为
String third = str.substring(i + 1, j);
因为 substring 方法的第二个参数是结束索引(不包含在子字符串内),即如果你想要子字符串包括索引 j-1,你需要将参数设置为 j。
英文:
Two things:
- The choice of i and j, i = 1, and j = 3 (basically the indices of the letters to be swapped).
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论