一个简单的程序,需要看看哪里有错误:)

编程入门 行业动态 更新时间:2024-10-28 15:28:18
本文介绍了一个简单的程序,需要看看哪里有错误:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

问候,这是我编程的第一年和第3周的这个 所以算我是一个纯粹的新手。 我正在尝试制作一个程序一个数字和打印开始 如下: 输入3: * ** *** 输入6: * ** *** **** ***** ****** 正如您所看到的那样,我尝试制作的程序会产生与输入数字相同的行数 ,每行增加一颗星。 这个是我写的程序: #include< stdio.h> star(int x) { int i = 1; while(i< = x) { printf(" ; *"); i ++; } } print(int x ){ int start = 1; int end = x; while(start< = end) { printf("%d \ n",sta r(开始)); 开始++; } } main() { printf(输入值\ n); int veri; scanf("%d" ,& veri); print(veri); } 似乎有效,但有一个错误: 我总是在这样的星星后得到一个数字: Ex: 输入值 8 * 2 ** 3 *** 4 **** 5 ***** 6 ****** 7 ******* 8 ** ****** 9 提前致谢:)

Greetings , This is my first year at programming and 3rd week at this so count me as a pure newbie. I am trying to make a program that gets a number and prints out starts as this: For input 3 : * ** *** For input 6: * ** *** **** ***** ****** As you see the program I am trying to make generates as much as lines as the input number , one star increasement at each line. This is the program I wrote: #include <stdio.h> star(int x) { int i=1; while(i<=x) { printf("*"); i++; } } print(int x){ int start = 1; int end = x; while (start <= end) { printf("%d \n", star(start) ); start++; } } main() { printf("Enter value \n"); int veri; scanf("%d", &veri); print(veri); } Seems works but with a bug: I always get a number after stars such as this: Ex: Enter value 8 *2 **3 ***4 ****5 *****6 ******7 *******8 ********9 Thanks in advance :)

推荐答案

Odinn写道: Odinn wrote: 问候,这是我编程的第一年和第3周的这个 所以算我是一个纯粹的新手。 /> 我正在尝试创建一个获得数字并打印出来的程序 如下: 输入3: * ** *** 输入6: * ** *** **** ***** ****** 正如您所看到的我正在尝试的程序生成尽可能多的行 作为输入数字,每行增加一个星。 这是我写的程序: #include< stdio.h> star(int x) { int i = 1 ; while(i< = x) { printf(" *"); i ++; } } print(int x){ int start = 1 ; int end = x; while(start< = end) { printf(" ;%d \ n",star(start)); start ++; } } main() { printf(" Enter value \ n"); int veri; scanf("%d",& veri); print(veri); } 似乎有效,但有一个错误: 我总是在这样的星星后得到一个数字: Ex: 输入值 8 * 2 ** 3 *** 4 **** 5 ***** 6 ****** 7 ******* 8 ******** 9 提前致谢:) Greetings , This is my first year at programming and 3rd week at this so count me as a pure newbie. I am trying to make a program that gets a number and prints out starts as this: For input 3 : * ** *** For input 6: * ** *** **** ***** ****** As you see the program I am trying to make generates as much as lines as the input number , one star increasement at each line. This is the program I wrote: #include <stdio.h> star(int x) { int i=1; while(i<=x) { printf("*"); i++; } } print(int x){ int start = 1; int end = x; while (start <= end) { printf("%d \n", star(start) ); start++; } } main() { printf("Enter value \n"); int veri; scanf("%d", &veri); print(veri); } Seems works but with a bug: I always get a number after stars such as this: Ex: Enter value 8 *2 **3 ***4 ****5 *****6 ******7 *******8 ********9 Thanks in advance :)

不确定你想要完成什么通过在printf中调用star 函数。也许我错过了什么。如果你只需要把它拿出去并在你的明星 函数中添加新行的打印,它似乎输出你所描述的想要的东西: #include< stdio.h> star(int x) { int i = 1; while(i< = x) { printf(" *"); i ++; } printf(" \ n"); } printstar(int x){ int start = 1; int end = x; while(start< = end) { star(start); start ++; } } main() { printf("输入值\ n"); int veri; scanf("%d",& veri); printstar(veri); }

Not sure what you were trying to accomplish by calling the star function from within the printf. Maybe I am missing something. If you just take that out and add the printing of the new line in your star function it seems to output what you described as wanting: #include <stdio.h> star(int x) { int i=1; while(i<=x) { printf("*"); i++; } printf("\n"); } printstar(int x){ int start = 1; int end = x; while (start <= end) { star(start) ; start++; } } main() { printf("Enter value \n"); int veri; scanf("%d", &veri); printstar(veri); }

Odinn< da ******* @ gmailwrote: Odinn <da*******@gmailwrote: 问候,这是我编程的第一年第3周这个 所以算我是纯新手。 Greetings , This is my first year at programming and 3rd week at this so count me as a pure newbie.

嗯,你至少尝试过,这是一个很好的开始。还不错 所有,但有一些事情需要注意:

Well, you''ve tried at least, and that''s a great start. Not bad at all, but there are some things to note:

#include< stdio.h> #include <stdio.h>

star(int x) star(int x)

这是有效的 - 返回类型默认为" INT"如果你没有明确地提供 - 但这不是一个好主意,它也不是最新的C语言标准所支持的。鉴于你没有给b $ b返回任何东西,也许是无效。在这里是合适的。

This is valid - the return type defaults to "int" if you don''t supply one explicitly - but it isn''t a great idea, and it also isn''t supported by the latest C language standard. Given that you don''t return anything, perhaps "void" would be appropriate here.

{ int i = 1; while(i< = x) { printf(" *"); i ++; } } { int i=1; while(i<=x) { printf("*"); i++; } }

print(int x){ int start = 1; int end = x; while(start< = end) { printf("%d \ n",明星(开始)); print(int x){ int start = 1; int end = x; while (start <= end) { printf("%d \n", star(start) );

这里是你看到的那些数字进来的地方。在printf中的%d() 告诉它看起来对于一个数字,这是星(开始)返回的。 因为你实际上没有返回任何东西,所以 被打印的数字不能被预测。请注意这一点。 star(start); printf(" \ n"); $ b如果没有打印那个数字,$ b就可以很好地工作。

Here''s where those numbers you''re seeing come in. That %d in printf() tells it to look for a number, which is what star(start) returns. Since you didn''t actually return anything, though, the number that gets printed can''t be predicted. Watch out for that. star(start); printf( "\n" ); will work quite nicely without printing that number.

start ++; } } start++; } }

main() main()

这样会更好 int main(无效) ....

This would be better as int main(void) ....

{ printf (输入值\ n); int veri; scanf("%d",& veri); print(veri); { printf("Enter value \n"); int veri; scanf("%d", &veri); print(veri);

....这意味着你会想要在这里返回一些东西。 0表示成功, 所以 返回0;

....which means you''ll want to return something here. 0 means success, so return 0;

} }

可能比我的第一个C程序好... - C. Benson Manica |我*应该*知道我在说什么 - 如果我 cbmanica(at)gmail |不,我需要知道。火焰欢迎。

Probably better than my first C program... -- C. Benson Manica | I *should* know what I''m talking about - if I cbmanica(at)gmail | don''t, I need to know. Flames welcome.

非常感谢克里斯托弗先生的帮助,以及帮助。真的 赞赏。 Uluc Aydin Christopher Benson-Manica yazdi: Thanks alot for the help Mr Christopher , aswell as the help. Really appreciated. Uluc Aydin Christopher Benson-Manica yazdi: Odinn< da ******* @ gmailwrote: Odinn <da*******@gmailwrote: 问候,这是我编程的第一年和第3周> 所以算我是一个纯粹的新手。 Greetings , This is my first year at programming and 3rd week at this so count me as a pure newbie.

嗯,你至少尝试过,这是一个很好的开始。还不错 所有,但有一些事情需要注意:

Well, you''ve tried at least, and that''s a great start. Not bad at all, but there are some things to note:

#include< stdio.h> #include <stdio.h>

star(int x) star(int x)

这是有效的 - 返回类型默认为" INT"如果你没有明确地提供 - 但这不是一个好主意,它也不是最新的C语言标准所支持的。鉴于你没有给b $ b返回任何东西,也许是无效。在这里是合适的。

This is valid - the return type defaults to "int" if you don''t supply one explicitly - but it isn''t a great idea, and it also isn''t supported by the latest C language standard. Given that you don''t return anything, perhaps "void" would be appropriate here.

{ int i = 1; while(i< = x) { printf(" *"); i ++; } } { int i=1; while(i<=x) { printf("*"); i++; } }

print(int x){ int start = 1; int end = x; while(start< = end) { printf("%d \ n",明星(开始)); print(int x){ int start = 1; int end = x; while (start <= end) { printf("%d \n", star(start) );

这里是你看到的那些数字进来的地方。在printf中的%d() 告诉它看起来对于一个数字,这是星(开始)返回的。 因为你实际上没有返回任何东西,所以 被打印的数字不能被预测。请注意这一点。 star(start); printf(" \ n"); $ b如果没有打印那个数字,$ b就可以很好地工作。

Here''s where those numbers you''re seeing come in. That %d in printf() tells it to look for a number, which is what star(start) returns. Since you didn''t actually return anything, though, the number that gets printed can''t be predicted. Watch out for that. star(start); printf( "\n" ); will work quite nicely without printing that number.

start ++; } } start++; } }

main() main()

这样会更好 int main(无效) ...

This would be better as int main(void) ...

{ printf(" Enter value \ n"); int veri; scanf("%d",& veri); print(veri); { printf("Enter value \n"); int veri; scanf("%d", &veri); print(veri);

...这意味着你会想要在这里归还一些东西。 0表示成功, 所以 返回0;

...which means you''ll want to return something here. 0 means success, so return 0;

} }

可能比我的第一个C程序好... - C. Benson Manica |我*应该*知道我在说什么 - 如果我 cbmanica(at)gmail |不,我需要知道。火焰欢迎。

Probably better than my first C program... -- C. Benson Manica | I *should* know what I''m talking about - if I cbmanica(at)gmail | don''t, I need to know. Flames welcome.

更多推荐

一个简单的程序,需要看看哪里有错误:)

本文发布于:2023-11-07 22:10:24,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有错误   简单   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!