学习qbasic并试图理解我刚写的代码 [英] Learning qbasic and im trying to understand the code I just wrote
问题描述
所以我遇到了一个问题,告诉我用星星打印一个三角形。
我最终猜测并检查并取出它。
So I had a problem on a hw assignment that told me to print a triangle out of stars.
I ended up guessing and checking and pulling out this.
DIM LINES AS INTEGER
CLS
INPUT "How many lines will your triangle be: ", LINES
FOR x = 1 TO LINES
PRINT
FOR y = 1 TO x
PRINT "*";
NEXT y
NEXT x
END
守则有效,但我不明白怎么做。我已经快速掌握了编程,但嵌套循环让我在大部分时间都感到困惑。我现在很难学习qbasic,但这就是我的学校提供的。有人可以解释我刚才写的代码。并帮助我DO和FOR命令。谢谢我,我的硬件非常好,所以我的老师没有帮助。
我尝试了什么:
我猜并检查了代码,现在我很困惑。我只是要求解释。尝试自我解释,但是,没有理解它。
The Code works but I dont understand how. I've picked up programming fast, but nested loops have left me confused most of the time. I now its weird to be learning qbasic but that is what my school offers. Can someone please explain the code I just wrote. And help me with DO, and FOR commands. Thank You I am really far ahead with the HW so my teacher isnt helping.
What I have tried:
I guess and checked the code and am now confused. An explanation is all im asking for. Tried to self explain but, didnt understand it.
推荐答案
我会在一张纸上开始,因为你只有3个变量,LINES,X,Y
我打算使用符号a< - b表示a的值变为b
LINES< - 5作为起点
X< - 1(满分5分)
Y< - 1 (超出X等于1)
打印*
第1行,打印一份*
X< - 2(满分5分)
Y< - 1(X中等于2)
打印*
Y< - 2(X中等于2)
打印*
第2行,两个*打印
X< - 3(满分5分)
Y< - 1(X中等于3)
打印*
Y< - 2(X中等于3)
打印*
Y< - 3(超出X等于3)
第3行,3*打印
所以对于我的情况5(线)中的每一行'X',我将使用'Y'来打印该行上的星数 - 等于X星 - 第一行有1星,第二行有2星,第三行有3星,第4行有4星,第5行有5星
btw,在两个循环之间XY即
I would have started on a piece of paper, since you only have 3 variables, LINES, X, Y
I'm going to use a notation a <- b means that a becomes the value of b
LINES <- 5 as a start point
X <- 1 (out of 5)
Y <- 1 (out of X which equals 1)
Print "*"
Line 1, one "*" printed
X <- 2 (out of 5)
Y <- 1 (out of X which equals 2)
Print "*"
Y <- 2 (out of X which equals 2)
Print "*"
Line 2, two "*" printed
X <- 3 (out of 5)
Y <- 1 (out of X which equals 3)
Print "*"
Y <- 2 (out of X which equals 3)
Print "*"
Y <- 3 (out of X which equals 3)
Line 3, 3 "*" printed
So for every line 'X' in my case 5 (Lines), Im going to use 'Y' to print the number of stars on that line - which equals X stars - 1st line has 1 star, 2nd has 2 stars, 3rd line has 3 stars, 4th line has 4 stars, 5th line has 5 stars
btw, in between the two loops X Y ie
NEXT Y
NEXT X
我会做类似的事情
I would have done something like
NEXT Y
PRINT (something to make a new line)
NEXT X
但是自从我做了任何BASIC以来已经很长时间了,所以你必须要看一下
有帮助吗?
如果你缩进,它也更容易看到'循环' em $ / $
but its been a long time since Ive done any BASIC so you'll have to look that up
does that help ?
Its also easier to see 'loops' if you indent them
FOR X = 1 to LINES
FOR Y = 1 to X
NEXT Y
NEXT X
[/ EDIT]
[/EDIT]
我建议使用调试器,但我不记得它是否在QBasic中有效。
从Standord University试试这个链接:
学习编程 [ ^ ]
和这一个
https://www.khanacademy.org/computing/computer-programming/programming/l ooping / p / nested-for-loops [ ^ ]
根据需要搜索教程。
I would recommend to use the debugger, but I don't remember if it was efficient in QBasic.
Try this link from Standord University:
Learn to Program[^]
and this one
https://www.khanacademy.org/computing/computer-programming/programming/looping/p/nested-for-loops[^]
Search for tutorials as you need.
这篇关于学习qbasic并试图理解我刚写的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!