这个C代码有什么问题? [英] What is wrong with this C code?

查看:75
本文介绍了这个C代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作一个星形图案的程序,同时给你一些行进入,例如:



1-输入2行:



*

**



2-输入3行:



*

**

***



3-输入4行:

*

**

***

****



对于每一行,星数增加一,我做了一个用C编写的程序,这样做却失败了(我知道我知道我的代码看起来很可怕但是请忍受它我仍然是初学者XD)



我尝试过:



So I am making a program which makes a star pattern while giving you a number of rows to enter , Example:

1- enter 2 rows :

*
**

2- enter 3 rows :

*
**
***

3- enter 4 rows :
*
**
***
****

For each next line the number of stars increase by one I did a program written in C which does that but it failed ("I know I know my code might seem horrible but please bear with it I am still a beginner XD)

What I have tried:

#include <stdio.h>
int main() {
int i = 0;
int b = 1;
int r;
int o = 0;
scanf("%d",&r);
int c = (r + b) - r;
while ( i < c) {
if (c == r) {
return 0;
}
while( o < c ) {
printf("*");
}
printf("/n");
i++;
b++;
c = (r + b) - r;
}
}

推荐答案

学会正确缩进代码,显示其结构,有助于阅读和理解。它还有助于发现结构错误。

Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
#include <stdio.h>
int main() {
    int i = 0;
    int b = 1;
    int r;
    int o = 0;
    scanf("%d",&r);
    int c = (r + b) - r;
    while ( i < c) {
        if (c == r) {
            return 0;
        }
        while( o < c ) {
            printf("*");
        }
        printf("/n");
        i++;
        b++;
        c = (r + b) - r;
    }
}



缩进风格 - 维基百科 [ ^ ]



专业程序员的编辑有这个功能和其他功能,如括号匹配和语法高亮。

Notepad++主页 [ ^ ]

ultraedit [ ^ ]



您的代码行为不符合您的预期,或者你不明白为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向您展示您的代码正在做什么你的任务是与它应该做的事情进行比较。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到bug,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



1.11 - 调试程序(步进和断点)|学习C ++ [ ^ ]



调试器仅显示您的代码正在执行的操作,您的任务是与应该执行的操作进行比较。


Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


Quote:

while( o < c ) {
    printf("*");
}

无数的星星......

An infinite number of stars ...


你为什么要在2019年学习C?你打算在哪里使用它?



我不知道你为什么用所有这些代码捆绑自己。这有效:



Why are you learning C in 2019? Where do you intend to use it?

I'm not sure why you've tied yourself in knots with all this code. This works:

#include <stdio.h>
int main() {
int r;
scanf("%d",&r);

for(int i = 0; i < r; ++i)
{
printf("*");

}

printf("\n");

return 0;
}


这篇关于这个C代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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