英文:
Removing whitespace at the end
问题
public class Evens {
public static String evens(int a) {
if (a < 0) {
return "NONE";
}
String num = "";
for (int i = 0; i <= a; i += 2) {
num += i + " ";
}
return num.trim();
}
}
英文:
I have to write a Java method that will return the following as a string:
Example: evens(5); String returned by method: “0 2 4”
Example: evens(8); String returned by method: “0 2 4 6 8”
Example: evens(-5); String returned by method: “NONE”
Example: evens(0); String returned by method: “0”
The problem I am encountering is a whitespace issue at the end of my code. If my code prints evens(5) the string returned by my method will be "0 2 4 ", but I need it to look like the examples above. My code looks like this:
public class Evens {
public static String evens(int a) {
if (a < 0) {
return "NONE";
}
String num = "";
for (int i = 0; i <= a; i += 2) {
num += i + " ";
}
return num;
}
}
答案1
得分: 0
不确定您使用的是哪种语言。看起来像是Java(或者C#?)。String类通常有一个trim()
或strip()
方法,它可以移除开头和结尾的空白字符。
如果您使用的是C#,考虑使用string.join()
和Enumerable.Range()
。
英文:
Not sure what language you are using. Looks like Java (c# ?). String classes usually have a trim()
or strip()
method which will remove leading and trailing whitespace.
If you are using c#, consider using string.join()
and Enumerable.Range()
答案2
得分: 0
要去除尾部空白,您需要在num
的最终值上调用该方法。如果您遇到问题,可能是因为在进行修剪时可能会丢失空格。这是您应该将返回语句更改为的内容。
return num.stripTrailing();
在String类中有一些处理标准修剪功能的方法。值得熟悉Java文档,这是Java 11中的String文档。
或者您可以收集数组中的所有偶数,然后使用String.join()构建最终的返回字符串。
英文:
To remove the trailing whitespace you'll need to invoke the method on the final value of num
. If you are having issues it might be because you have been trimming as you go and so loosing the spaces in between. This is what you should change your return statement to be.
return num.stripTrailing();
There are a few methods in the String class that handle the standard trim type functionality. It's worth getting familiar with the Java docs, here's String in Java 11
Alternatively you may want to gather all the even numbers in an array then use String.join() to build the final string that gets returned.
答案3
得分: 0
比起去掉最后的空格,更加优雅的做法是一开始就不要添加它。只需检查是否处于循环的最后一次迭代,如果是,则不添加分隔符。
for (int i = 0; i <= a; i += 2)
{
if(i < a - 1)
num += i + " ";
else
num += i;
}
另一个常用的技巧是仅在前 n-1
次迭代中添加分隔符,然后仅打印最后一个值而无需分隔符,从而省去了循环内的检查。
int i;
for (i = 0; i < a - 1; i += 2)
{
num += i + " ";
}
num += i;
注意我们必须在 for
语句外部声明循环变量 i
,以使其可见。
最后,您可以考虑使用 StringBuffer
。
英文:
More elegant than stripping off the final space is to not add it in the first place. Just check if you're at the last iteration of the loop and if so don't add the delimiter.
for (int i = 0; i <= a; i += 2)
{
if(i < a - 1)
num += i + " ";
else
num += i;
}
Another common trick is to only add a delimiter on the first n-1
iterations, then just print the final value with no delimiter, doing away with the need for a check in the loop.
int i;
for (i = 0; i < a - 1; i += 2)
{
num += i + " ";
}
num += i;
Notice how we have to declare the loop variable i
outside of the for
statement to make it visible.
Finally, you might consider using a StringBuffer
.
答案4
得分: 0
你不需要在偶数后面添加空格,只需要在它们前面加一个空格,这样你就不必担心尾随空格。
- 首先将你的默认答案设置为0,这样如果
for循环
不执行,它将返回0。 - 如果方法参数的值小于0,只需返回 NONE。
- 如果都没有执行,那么意味着你有一个有效的范围,然后就是基本的
for循环
逻辑。
public static String even(int amount) {
String output = "0";
if(amount < 0) {
return "NONE";
}
else if(amount == 0) {
return output;
}
for(int i=2; i<=amount; i += 2) {
output += " " + i;
}
return output;
}
public static void main(String[] args) {
System.out.println(even(5));
System.out.println(even(8));
System.out.println(even(-5));
System.out.println(even(0));
}
英文:
You don't have to append a space after the even number, you can just put a space before them, so you don't have to worry about trailing spaces.
-
First is to set your default answer as 0, so that if the
for-loop
doesn't execute it will return 0. -
If value of method argument is less than 0 just return NONE
-
If both are not executed, that means you have a valid range, then its a basic
for-loop
logicpublic static String even(int amount) { String output = "0"; if(amount < 0) { return "NONE"; } else if(amount == 0) { return output; } for(int i=2; i<=amount; i += 2) { output += " " + i; } return output; } public static void main(String[] args) { System.out.println(even(5)); System.out.println(even(8)); System.out.println(even(-5)); System.out.println(even(0)); }
答案5
得分: 0
你可以使用String.join来获取所需的序列。可以使用以下代码:
private static void findEvenSequences(int max) {
String exp = new String();
if (max < 0) {
exp = "NONE";
} else {
for (int i = 0; i < max; i = i + 2) {
exp = String.join(" ", exp, String.valueOf(i));
}
}
System.out.println(exp.trim());
}
英文:
You can use String.join to get the required sequence. You can use below Code
private static void findEvenSequences(int max) {
String exp = new String();
if (max < 0) {
exp = "NONE";
} else {
for (int i = 0; i < max; i = i + 2) {
exp = String.join(" ", exp, String.valueOf(i));
}
}
System.out.println(exp.trim());
}
专注分享java语言的经验与见解,让所有开发者获益!
评论