Qbasic偶数或奇数发生器.有一个陷阱. [英] Qbasic even or odd number generator. There is a catch.

查看:219
本文介绍了Qbasic偶数或奇数发生器.有一个陷阱.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个关于编程课的问题.问题如下.无法弄清楚该怎么做.我的老师说要使用数组,但我不知道怎么做.


编写一个程序,以选择20个随机数,介于10和99之间(含10和99).然后将数字打印出来,一行上的所有偶数,另一行上的所有赔率.输出应如下所示:

奇数:49 25 21 33 63 77 29 77 27 43
偶数:66 56 18 24 28 70 56 62 48 76

这就是我的程序当前的样子.

随机计时器

对于x = 1到20
z = INT(RND * 89)+ 10
如果z MOD 2 = 0转到7否则转到12
7如果x = 1 THEN
打印偶数:"; z;
ELSE
PRINT z;
END IF
12如果x = 1 THEN
打印"ODD:"; z;
ELSE
PRINT z;
END IF
NEXT x
END

不知道为什么它不起作用.我对GOTO不太熟悉,所以我很可能在那儿做错了.

输出必须看起来与问题的产生方式完全相同.因此,CPU必须在第一行上打印ODD,在第二行上打印EVEN,并在其右边显示数字.

如果可能的话,我想要一个数组定义和解决方案以及我当前未完成的数组解决方案.

我尝试过的事情:

我已经尝试过数组和goto了,但是我只是这个问题的基本知识.

So I have a question for my programming class. Question Below. Cant figure out how to do it. My teacher said to use an array but I don''t know how.


Write a program which picks 20 random numbers between 10 and 99 inclusive. The numbers are then printed out with all the evens on one line and all odds on the other line. The output should look like:

ODD: 49 25 21 33 63 77 29 77 27 43
EVEN: 66 56 18 24 28 70 56 62 48 76

So this is what my program currently looks like.

RANDOMIZE TIMER

FOR x = 1 TO 20
z = INT(RND * 89) + 10
IF z MOD 2 = 0 GOTO 7 ELSE GOTO 12
7 IF x = 1 THEN
PRINT "EVEN: "; z;
ELSE
PRINT z;
END IF
12 IF x = 1 THEN
PRINT "ODD: "; z;
ELSE
PRINT z;
END IF
NEXT x
END

Not sure why it isnt working. Im not really that familiar with GOTO so I''m probobly doing something wrong there.

The Output has to look exactly the same as the way the question had it. So the cpu has to print ODD on first line and EVEN on second line with their numbers to the right.

If possible I would like an array definition and solution as well as my current non array finished solution.

What I have tried:

I have attempted arrays and goto but I''m just to basic for this question.

推荐答案

我会剔除GOTO并重新思考该方法..
呢?
1)生成20个随机数组成的数组

I''d chuck out the GOTO''s and rethink the approach .. what about

1) generating an array of 20 random numbers

RANDOMIZE TIMER
' Declare an array to hold all the random numbers
Dim AllNumbers As INTEGER
' Generate 20 Random numbers in the correct range
FOR x = 1 TO 20
    z = INT(RND * 89) + 10
    AllNumbers(x) = z
NEXT



2)从这里可以有两种方法-两个子例程,一个用于奇数,一个用于偶数,或者两个循环,一个用于打印赔率,一个用于打印偶数..这是一个示例



2) There are two ways you can go from here - either two subroutines, one for Odd, one for Even, or, two loops, one to print the odds, one to print the evens .. here''s an example

' Print the ODD's 
PRINT "ODD:";
' Loop Over the Array
FOR x = 1 to 20
    IF AllNumbers(x)  MOD 2 != 0 THEN PRINT AllNumbers(x);
NEXT
PRINT



甚至"是类似的...(我不记得如何在打印语句中跳到新行",您必须向上看一个,我想它是否是分号) ...并且您可能会需要在数字之间留出空格,同样,您可以查找一些东西



''Even'' is similar ... (I cant remember how to ''jump to a new line'' in a print statement, you''ll have to look that one up, I think its semi-colons or not) ... and you''ll likely need spaces between the numbers, again, something you can look up


我过去20多年或更长时间没有使用QBasic.

只是一个建议:使用调试器,您将看到代码正在运行.

您的代码与该语句不匹配,需要完全重写.了解如何使用数组.
I have not used QBasic for last 20 years or more.

Just an advice: use the debugger and you will see your code running.

Your code do not match the statement and need a complete rewrite. See how to use arrays.


这篇关于Qbasic偶数或奇数发生器.有一个陷阱.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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