如何在objective-c中水平打印值

时间:2021-03-05 22:49:29

I try to print loop of numbers in order For example:

我尝试按顺序打印数字循环例如:

123

123

instead i get :

相反,我得到:

1
2
3

1 2 3

how i can do that ?

我怎么能这样做?

void enterNum(int num){
    int x=1;
    for (int i=1; i<num; i++) {
        for (int z=1; z<num; z++) {
            int sum = z*x;
            NSLog(@"%i",sum);
        }
        NSLog(@"\n");
        x++;
    }
}

2 个解决方案

#1


2  

NSLog prints every time in a new line, so you have to store the value, before printing it out. Try this:

NSLog每次都在新行中打印,因此您必须在打印之前存储该值。尝试这个:

void enterNum(int num){
    int x=1;

    // Added mutable string here
    NSMutableString *logString = [NSMutableString new];

    for (int i=1; i<num; i++) {

        for (int z=1; z<num; z++) {
            int sum = z*x;

            // Moved logging from here and appended value to logString
            [logString appendFormat:@"%i", sum];
        }

        // Log the total string here
        NSLog(@"%@\n", logString);

        // Clear the logString
        [logString setString:@""];
        x++;
   }
}

#2


0  

You could just use the C print function: printf("%i", sum); printf does not add a new line. The downside to this is that it doesn't print Objective - C objects i.e. you cannot use %@.

你可以使用C打印功能:printf(“%i”,sum); printf不添加新行。这样做的缺点是它不打印Objective-C对象,即你不能使用%@。

#1


2  

NSLog prints every time in a new line, so you have to store the value, before printing it out. Try this:

NSLog每次都在新行中打印,因此您必须在打印之前存储该值。尝试这个:

void enterNum(int num){
    int x=1;

    // Added mutable string here
    NSMutableString *logString = [NSMutableString new];

    for (int i=1; i<num; i++) {

        for (int z=1; z<num; z++) {
            int sum = z*x;

            // Moved logging from here and appended value to logString
            [logString appendFormat:@"%i", sum];
        }

        // Log the total string here
        NSLog(@"%@\n", logString);

        // Clear the logString
        [logString setString:@""];
        x++;
   }
}

#2


0  

You could just use the C print function: printf("%i", sum); printf does not add a new line. The downside to this is that it doesn't print Objective - C objects i.e. you cannot use %@.

你可以使用C打印功能:printf(“%i”,sum); printf不添加新行。这样做的缺点是它不打印Objective-C对象,即你不能使用%@。