我对Objective-C还是陌生的,因此我开始学习简单的东西。在这种情况下,我想使用pyramid
符号创建一个*
。
问题:每次打印*
时,都会出现换行符。如何摆脱它来创建金字塔外观?我以为我必须添加\n
才能在添加的Break line
文本中引起换行。
现在,我有以下代码创建金字塔:
//Pyramid
int a,b;
int x=5;
for(a = 1; a <= x; a++){
for(b=1; b<=a; b++){
NSLog(@"*");
}
NSLog(@"Break line");
}
结果:
2014-02-23 10:44:35.264 Pyramid[614:303] *
2014-02-23 10:44:35.266 Pyramid[614:303] Break line
2014-02-23 10:44:35.267 Pyramid[614:303] *
2014-02-23 10:44:35.267 Pyramid[614:303] *
2014-02-23 10:44:35.267 Pyramid[614:303] Break line
2014-02-23 10:44:35.268 Pyramid[614:303] *
2014-02-23 10:44:35.268 Pyramid[614:303] *
2014-02-23 10:44:35.269 Pyramid[614:303] *
2014-02-23 10:44:35.269 Pyramid[614:303] Break line
2014-02-23 10:44:35.269 Pyramid[614:303] *
2014-02-23 10:44:35.270 Pyramid[614:303] *
2014-02-23 10:44:35.270 Pyramid[614:303] *
2014-02-23 10:44:35.270 Pyramid[614:303] *
2014-02-23 10:44:35.271 Pyramid[614:303] Break line
2014-02-23 10:44:35.271 Pyramid[614:303] *
2014-02-23 10:44:35.272 Pyramid[614:303] *
2014-02-23 10:44:35.272 Pyramid[614:303] *
2014-02-23 10:44:35.272 Pyramid[614:303] *
2014-02-23 10:44:35.273 Pyramid[614:303] *
2014-02-23 10:44:35.273 Pyramid[614:303] Break line
最佳答案
如注释中所述,NSLog将始终仅输出一行,并且您将无法避免像在Java中那样进行操作。另外,您应该研究构建字符串(例如此处描述的Shortcuts in Objective-C to concatenate NSStrings)。
一个例子:
//Pyramid
int a,b;
int x=5;
for(a = 1; a <= x; a++){
NSMutableString *string = [NSMutableString string];
for(b=1; b <= a; b++){
[string appendString:@"*"];
}
NSLog(@"%@", string);
}
关于ios - NSLog默认中断线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21963631/