英文:
How to fix loop so that the code does not print "+" behind the last number
问题
我不知道如何表达标题,所以请随意进行更改!我被困在这个涉及循环的程序中。我希望输入是1 5
,这意味着代码从1开始一直加到5,预期的输出应该是1 + 2 + 3 + 4 + 5 = 15
,但我的代码(见下文)会打印出类似1 + 2 + 3 + 4 + 5 + = 15
的内容。我该如何去掉这个不需要的加号符号?
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int start = keyboard.nextInt();
int end = keyboard.nextInt();
double dend = (double)end;
double dstart = (double)start;
double n = (dend - dstart + 1);
double sum = (n/2)*(dend+dstart);
int intsum = (int)sum;
for (int i =start; i <= end; i++) {
System.out.print(i + " + ");
}
System.out.print(" = " + intsum);
}
英文:
I have no idea how to phrase the title so feel free to make changes! I am stuck on this program that I have made involving a loop. I want the input to be 1 5
which means the code starts adding from 1 and all the way until 5, the expected output would be 1 + 2 + 3 + 4 + 5 = 15
but my code (see below) would print something like 1 + 2 + 3 + 4 + 5 + = 15
. How do I get rid of that unwanted addition symbol?
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int start = keyboard.nextInt();
int end = keyboard.nextInt();
double dend = (double)end;
double dstart = (double)start;
double n = (dend - dstart + 1);
double sum = (n/2)*(dend+dstart);
int intsum = (int)sum;
for (int i =start; i <= end; i++) {
System.out.print(i+" + ");
}
System.out.print(" = "+intsum);
}
答案1
得分: 1
你可以将循环次数减少一个,并再次打印出结果:
for (int i = start; i < end; i++) {
System.out.print(i + " + ");
}
System.out.print(end);
System.out.print(" = " + intsum);
或者你可以在循环中使用 if
逻辑:
for (int i = start; i <= end; i++) {
System.out.print(i);
if (i != end) System.out.print(" + ");
}
英文:
You could reduce the looping by one, and then print again
for (int i =start; i < end; i++) {
System.out.print(i+" + ");
}
System.out.print(end);
System.out.print(" = "+intsum);
or you could have if
logic in your loop
for (int i =start; i <= end; i++) {
System.out.print(i);
if (i != end) System.out.print(" + ");
}
答案2
得分: 0
逻辑可以是这样的:
for (int i = start; i <= end; i++)
{
if (i > start)
System.out.print(" + ");
System.out.print(i);
}
这个想法是在循环的第一次迭代之后才打印“+”。
另一个选项是使用StringJoiner:
StringJoiner sj = new StringJoiner(" + ");
for (int i = start; i <= end; i++)
{
sj.add(i + ""); // 将“i”转换为字符串
}
System.out.print(sj.toString());
StringJoiner会根据需要自动为您添加分隔符。
英文:
The logic could be something like:
for (int i =start; i <= end; i++)
{
if (i > start)
System.out.print(" + ");
System.out.print(i);
}
The idea is you only print "+" AFTER the first iteration of the loop.
Another option is to use a StringJoiner:
StringJoiner sj = new StringJoiner(" + ");
for (int i =start; i <= end; i++)
{
sj.add( i + "" ); // converts the "i" to a string
}
System.out.print( sj.toString() );
The StringJoiner will add the delimiter for you automatically as required.
答案3
得分: 0
正如Scary Wombat所提到的,您可以将循环减少一个,但在您的代码中,您对每个元素进行了字符串连接,这是不好的(因为Java中字符串的不可变性质)。因此,最好使用一些可变的字符串对象(可以是StringBuilder/StringBuffer),如果您想使用流来实现这个,还可以看看Java8引入的Collectors.joining()。
英文:
As already mentioned by Scary Wombat you can reduce the lopping by one but in your code you are doing string concatenation for each element which not good (because of immutability nature of string in java).
So better to use some mutable string object (either StringBuilder/StringBuffer) and also you can have look at Collectors.joining() (which is introduced in Java8) if you would like implement this using streams.
答案4
得分: 0
一个替代方案是计算迭代次数,并检查是否在最后一次迭代。如果不是,则打印一个“+”。
英文:
An alternate solution would be to count the number of iterations and check if it is on it's last iteration. If not, then print a "+".
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论