如果我们在print语句中放一个空格,然后打印变量,那么java中的Why For循环会给出不同的结果


public class Countingtable {
public static void main(String[] args) {
// write a program to count/increase a number by 2 everytime.
int number1;
for (number1 = 2; number1 <= 20; number1++)
System.out.println("this number is a multiple of 2  " + number1);
}
}

请注意,如果我删除打印语句中的空格,会产生不同的结果吗?任何线索都会对有所帮助并表示赞赏

System.out.println("this number is a multiple of 2" + number1);

这个很有趣。你可以看到,当你放置空间时,输出是你想要的

This number is multiple of 2 2
This number is multiple of 2 3
This number is multiple of 2 4

但当你去掉空格时,答案仍然正确,但它与语句中的数字2相连,即

This number is multiple of 22 -- here 2 is from the statement and the next one is from answer
This number is multiple of 23 -- here 2 is from the statement and the next one is from answer
This number is multiple of 23 -- here 2 is from the statement and the next one is from answer

最新更新