Java-从for循环返回信息
所以我想做的是返回: 输入是Java-从for循环返回信息,java,loops,for-loop,return,Java,Loops,For Loop,Return,所以我想做的是返回: 输入是createMixedString(您好,这里是3) 我想输出hellotherehellothere 我的问题是,当它运行时,它只返回hellother,就好像程序没有看到我在for循环中所做的重新分配一样 public static String createMixedString(String s1, String s2, int n) { String result = s1+s2; for (int i=0;i>=n;i++) {
createMixedString(您好,这里是3)
我想输出hellotherehellothere
我的问题是,当它运行时,它只返回hellother
,就好像程序没有看到我在for循环中所做的重新分配一样
public static String createMixedString(String s1, String s2, int n) {
String result = s1+s2;
for (int i=0;i>=n;i++) {
result = result+s1+s2;
}
return result;
}
0>=3
条件从不满足。它应该i
。因为我从0开始,所以它应该是0>=3
条件从未满足过。它应该i
。因为我从0开始,它应该是也许循环条件i>=n
应该是i也许循环条件i>=n
应该是i因为你的条件是错误的,它应该是i,如:
public static String createMixedString(String s1, String s2, int n) {
String result = s1+s2;
for (int i=0; i < n; i++) {
result = result+s1+s2;
}
return result;
}
公共静态字符串createMixedString(字符串s1、字符串s2、整数n){
字符串结果=s1+s2;
对于(int i=0;i
对于您的情况是错误的,它应该是i,如:
public static String createMixedString(String s1, String s2, int n) {
String result = s1+s2;
for (int i=0; i < n; i++) {
result = result+s1+s2;
}
return result;
}
公共静态字符串createMixedString(字符串s1、字符串s2、整数n){
字符串结果=s1+s2;
对于(int i=0;i
考虑以下几点:
public static String createMixedString(String s1, String s2, int n) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(s1);
s.append(s2);
}
return s.toString();
}
public static String createMixedString(String s1, String s2, int n) {
String result = s1 + s2;
return StringUtils.repeat(result, n);
}
公共静态字符串createMixedString(字符串s1、字符串s2、整数n){
StringBuilder s=新的StringBuilder();
对于(int i=0;i
请注意,条件中的检查会检查i
是否仍然小于n
,而不是检查i>=n
,这是没有意义的。此外,如果要连接字符串,则使用StringBuilder将更加有效。考虑以下几点:
public static String createMixedString(String s1, String s2, int n) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(s1);
s.append(s2);
}
return s.toString();
}
public static String createMixedString(String s1, String s2, int n) {
String result = s1 + s2;
return StringUtils.repeat(result, n);
}
公共静态字符串createMixedString(字符串s1、字符串s2、整数n){
StringBuilder s=新的StringBuilder();
对于(int i=0;i
请注意,条件中的检查会检查i
是否仍然小于n
,而不是检查i>=n
,这是没有意义的。此外,如果您连接字符串,使用StringBuilder将更加有效。您的循环结束条件是问题所在,将其更改为i您的循环结束条件是问题所在,将其更改为i为什么不使用StringUtils。重复该操作将为您执行相同的操作,以便您可以执行以下操作:
public static String createMixedString(String s1, String s2, int n) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(s1);
s.append(s2);
}
return s.toString();
}
public static String createMixedString(String s1, String s2, int n) {
String result = s1 + s2;
return StringUtils.repeat(result, n);
}
这应该按照您想要的方式工作。为什么不使用StringUtils。重复一下,它将为您执行相同的操作,以便您可以执行以下操作:
public static String createMixedString(String s1, String s2, int n) {
StringBuilder s = new StringBuilder();
for (int i = 0; i < n; i++) {
s.append(s1);
s.append(s2);
}
return s.toString();
}
public static String createMixedString(String s1, String s2, int n) {
String result = s1 + s2;
return StringUtils.repeat(result, n);
}
这应该按照你想要的方式工作检查条件,我认为你把“>”放错了方向。检查条件,我认为你把“>”放错了方向。