如何打印1到10之间的数字,不包括7? [英] How do I print numbers from 1 to 10 not including 7?

查看:529
本文介绍了如何打印1到10之间的数字,不包括7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够打印1到10号,但我不知道如何排除7!

请尽快帮助



我尝试过:



I was able to print out number 1 through 10 but i dont know how to exclude 7!
please help ASAP

What I have tried:

#include <stdio.h>
int main()
{
    int i;

    for (i=1; i <= 10; i++; i!=7)
    {
        printf("%d\t",i);
    }
    return(0);

}

推荐答案

for (i=1; i <= 10; i++; i!=7)



你不能只为for循环(或其他任何东西)编写语法,并希望它有效! br />


根据CHill60的评论,你需要一个'if'声明。更重要的是,您需要研究C的工作原理。 如果您是C新手并且只是学习,那么当您尝试编码时,您应该可以获得一个参考,这样您就可以看到不同的东西如何工作 - 然后进行实验和学习。我们都在某个时刻开始新的事物。你这样做的方式是编码屏幕和打开的书一起工作。也许是谷歌的教程,可以指导你完成任务。



随意尝试一些东西需要很多经验(至少在编码方面),即使这样也不是随意的由于经验。


You cannot just make up syntax for a "for" loop (or anything else) and hope it works!

Per CHill60's comment, you need an 'if' statement. More importantly, you need to study on how C works. If you're new to C and just learning, you should have a reference available to you as you try to code so you can see how the different things are made to work - then experiment and learn. We all start out new at some point. The way you do it is the coding screen and 'open book' working together. Perhaps Google for a tutorial to walk you through things.

Trying things at random requires a very lot of experience (in coding, at least) and even then it's not random due to the experience.


因为你付出了一些努力,我会帮助你。但请注意我在手机上这样做,所以我无法测试它。



稍微更改原始代码......

As you've put some effort in I'll help you out. But be aware I'm doing this on my phone so I can't test it.

Change your original code just a little...
#include <stdio.h> 
int main() 
{ 
int i;   
for (i=1; i <= 10; i++) 
{
      if (i!=7)
           printf("%d\t",i); 
} 
return(0);   
}


另一种方法是使用continue语句。

参见C编程中断并继续声明 - C教程 [ ^ ]



有时它使代码更容易阅读。 (当然有关于此的意见)



Yet another way to do it is to use the continue statement.
See C Programming break and continue Statement - C Tutorial[^]

Sometimes it makes for easier to read code. (There are of course opinions about that)

#include <stdio.h>
int main()
{
    int i;

    for (i=1; i <= 10; i++;)
    {
        if (i == 7)
            continue;    // This skips the rest of the statements 
                         // and starts the next iteration

        printf("%d\t", i);
    }
    return 0;
}</stdio.h>


这篇关于如何打印1到10之间的数字,不包括7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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