整数计划 [英] Integer Program

查看:58
本文介绍了整数计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是c的新手。我正在尝试编写一个简单的程序,它将提示用户输入1到9之间的整数,输出

将按如下方式生成(例如5)


1

22

333

4444

55555


我一直试图用while循环编写这个但是我已经得到了

卡住了。我正在使用这本书,C how to program 4th edition,但是

对于这个问题没有答案,而且这个

版本没有资源dietel网站。


如果有人能看到我到目前为止的代码(它不多 -

请记住我只是学习了这个几个星期)并提供

a建议如何让程序只打印数字

直到输入的整数这将是赞赏。


#include< stdio.h> / *用于库函数:printf * /


/ *函数main开始程序执行* /

main()

{

char string []; / *初始化* /


printf(输入1到9之间的整数:\ n);

scanf("%s" ,字符串);


/ *循环,直到用户键结束文件序列* /

while(string> = 1)&& (字符串< = 9);


} / *结束时* /

} / *结束函数main * /


谢谢

Hi
I am new to c. I am trying to write a simple program which will
prompt the user to enter an integer between 1 and 9 and the output
will be produced as follows (eg. 5)

1
22
333
4444
55555

I have been trying to write this with a while loop but I have gotten
stuck. I am using the book, "C how to program 4th edition", but there
is no answer for this question and there are no resources for this
edition on the dietel site.

If someone could look at the code that I have so far (it is not much -
please remember I have only been learning this a few weeks) and offer
a suggestion as to how I can get the program to print only the numbers
up to the integer entered this would be appreciated.

#include <stdio.h> /* for library function: printf */

/* function main begins program execution */
main()
{
char string[]; /* initialisation */

printf ( "Enter an integer between 1 and 9:\n" );
scanf ( "%s", string );

/* loop until user keys end-of-file sequence */
while ( string >= 1 ) && ( string <= 9);


} /* end while */
} /* end function main */

Thankyou

推荐答案



" emmx" < KR ****** @ emmx.com>在消息中写道

news:65 ************************** @ posting.google.c om ...

"emmx" <kr******@emmx.com> wrote in message
news:65**************************@posting.google.c om...

我是c的新手。我正在尝试编写一个简单的程序,它将提示用户输入1到9之间的整数,输出
将按如下方式生成(例如5)

1
22
我是一直尝试用while循环编写这个但是我已经卡住了。我正在使用这本书,C how to program 4th edition,但是这个问题没有答案,而且这个
版本没有资源在dietel网站上。
<如果有人可以查看我到目前为止的代码(它并不多 -
请记住我几周才学到这一点)并提供
建议我如何能够得到程序只打印数字
直到输入的整数这将是赞赏。

#include< stdio.h> / *用于库函数:printf * /

/ *函数main开始执行程序* /
main()
{char / string []; / * initialisation * /

printf("输入介于1和9之间的整数:\ n");
scanf("%s",string);

/ *循环直到用户键结束文件序列* /
while(string> = 1)&& (字符串< = 9);

} / *结束时* /
} / *结束函数main * /
Hi
I am new to c. I am trying to write a simple program which will
prompt the user to enter an integer between 1 and 9 and the output
will be produced as follows (eg. 5)

1
22
333
4444
55555

I have been trying to write this with a while loop but I have gotten
stuck. I am using the book, "C how to program 4th edition", but there
is no answer for this question and there are no resources for this
edition on the dietel site.

If someone could look at the code that I have so far (it is not much -
please remember I have only been learning this a few weeks) and offer
a suggestion as to how I can get the program to print only the numbers
up to the integer entered this would be appreciated.

#include <stdio.h> /* for library function: printf */

/* function main begins program execution */
main()
{
char string[]; /* initialisation */

printf ( "Enter an integer between 1 and 9:\n" );
scanf ( "%s", string );

/* loop until user keys end-of-file sequence */
while ( string >= 1 ) && ( string <= 9);


} /* end while */
} /* end function main */



这是一个解决方案:


#include< stdio.h> / *用于库函数:printf * /


/ *函数main开始程序执行* /

main()

{

int input = 0,i = 1,j; / *初始化* /


printf("输入介于1和9之间的整数:\ n");

fflush(stdout);

scanf("%i",& input);

getchar(); / *吸收潜伏在stdin中的换行符* /


while(i< = input)

{

for(j = 0; j< i; j ++)

{

printf("%i",i);

}

printf(" \ n");

i ++;

}

printf(" press return to exit" );

getchar(); / *打开consol窗口* /


} / *结束函数主* /


当然它不处理不正确的用户输入并且可能在编译器上表现不同
..



Here is one solution:

#include <stdio.h> /* for library function: printf */

/* function main begins program execution */
main()
{
int input=0,i=1,j; /* initialisation */

printf ( "Enter an integer between 1 and 9:\n" );
fflush(stdout);
scanf ( "%i", &input);
getchar(); /*absorb the newline character lurking in stdin*/

while (i<=input)
{
for(j=0;j<i;j++)
{
printf("%i",i);
}
printf("\n");
i++;
}
printf("press return to exit");
getchar(); /*hold the consol window open*/

} /* end function main */

Of course it does not deal with incorrect user input and may behave
differently on your compiler ..


这是我能想到的向您展示的最简单的方法。我很高兴你做了一些人的努力,因为有些人在询问如何做某事之前没有(在这个

新闻组):

--------------------------------

#include< stdio.h> / *对于printf()和scanf()* /


/ * main()必须是int类型,并返回一个int * /

int main()

{

int entry; / *保持输入的原始类型* /


printf(输入介于1和9之间的整数:);


/ * scanf()获取小数(%d)并将其输入(需要使用

地址&)* /

scanf(" %d",& entry);


/ *是1到9之间的条目? * /

if((entry> = 1)&&(entry< = 9))

{

/ * count到num直到我们达到entry-1 * /

的值为(int num = 0; num< = entry; ++ num)

{

/ *打印出num中的数字,直到达到num的值

* /

for(int iter = 0; iter< num ; ++ iter)

printf("%d",num);


printf(" \ n");

/ * fflush()确保输出显示* /

fflush(stdout);

}

}


/ *条目不在1到9之间,打印错误信息* /

else

printf(" Invalid Number!\\ \\ n");


/ *总是返回0 ...现在* /

返回(0);

}
This is the simplest way I could think of for showing you. I''m glad you made
the effort as some people do not before asking how to do something (on this
newsgroup):
--------------------------------
#include <stdio.h> /* For printf() and scanf() */

/* main() must be of type int, and return an int */
int main()
{
int entry; /* Primitive type to hold input */

printf ( "Enter an integer between 1 and 9: " );

/* scanf() gets a decimal ("%d") and puts it into entry (need to use the
address &) */
scanf ( "%d", &entry );

/* is entry between 1 and 9? */
if ( ( entry >= 1) && ( entry <= 9 ) )
{
/* count through num until we reach the value of entry-1 */
for ( int num = 0; num <= entry; ++num)
{
/* print out the number in num until the value of num is reached
*/
for ( int iter = 0; iter < num; ++iter)
printf ( "%d", num );

printf ("\n");
/* fflush() insures that the output is shown */
fflush(stdout);
}
}

/* entry was not between 1 and 9, print error message */
else
printf("Invalid Number!\n");

/* always return 0...for now */
return(0);
}


Greg P.写道:

Greg P. wrote:

printf("输入1到9之间的整数:" ;);

/ * scanf()得到一个小数(&q uot;%d")并将其输入(需要使用
地址&)* /
scanf("%d",& entry);
printf ( "Enter an integer between 1 and 9: " );

/* scanf() gets a decimal ("%d") and puts it into entry (need to use the
address &) */
scanf ( "%d", &entry );



只是一个注释:你想要一个''fflush(stdout);''

''\ n'''之间的声明 - 减去printf()和scanf()。

-

Martin Ambuhl



Just a note: You are wanting a ''fflush(stdout);'' statement between the
''\n''-less printf() and the scanf().
--
Martin Ambuhl


这篇关于整数计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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