如何prevent印刷空间的for循环的最后一个循环 [英] How to prevent printing space in the last loop of a for loop

查看:111
本文介绍了如何prevent印刷空间的for循环的最后一个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为你们一个简单的问题:

我写了这个循环,打印数组的一些值。
我想每一个值之间的空间,但我不希望有最后值之后一个空间,在code打印。

任何想法我如何做到这一点没有复杂太多了?

 为(i_talltrees = 1; i_talltrees< = numtrees; i_talltrees ++){
    pre = i_talltrees-1;
    后= i_talltrees + 1;
    如果(talltrees [pre< talltrees [i_talltrees&功放;&安培; talltrees [i_talltrees> talltrees [文章]){
        的printf(%d个,talltrees [i_talltrees]);
    }    }


解决方案

试试这个:

  INT标志= 0;
 如果(talltrees [pre< talltrees [i_talltrees&功放;&安培; talltrees [i_talltrees> talltrees [文章])
 {
    如果(标志)
    {
        的printf();
        标志= 0;
    }
    的printf(%d个,talltrees [i_talltrees]);
    标志= 1;
 }

a simple question for you guys:

I wrote this loop that prints some values of an array. i want space between every value but i would not like to have a space after the last value that the code prints.

any idea how do i do this without complicating too much?

for (i_talltrees=1; i_talltrees<=numtrees; i_talltrees++) {
    pre=i_talltrees-1;
    post=i_talltrees+1;
    if (talltrees[pre]<talltrees[i_talltrees]&&talltrees[i_talltrees]>talltrees[post]) {
        printf("%d ", talltrees[i_talltrees]);
    }

    }

解决方案

Try this:

 int flag = 0; 
 if (talltrees[pre]<talltrees[i_talltrees]&&talltrees[i_talltrees]>talltrees[post]) 
 {   
    if(flag)
    { 
        printf(" ");
        flag = 0;
    }
    printf("%d", talltrees[i_talltrees]); 
    flag = 1; 
 } 

这篇关于如何prevent印刷空间的for循环的最后一个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆