使用DO UNTIL LOOP从阵列打印最小数字
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用DO UNTIL LOOP从阵列打印最小数字相关的知识,希望对你有一定的参考价值。
我正在编写一个QBasic程序,它将使用DO UNTIL循环接收10名学生的分数并计算最低分数,但是当我运行它并输入10个学生分数时,我的程序似乎总是冻结。
我尝试了所有我知道的东西,但无论如何它仍然会冻结。
以下是我的代码:
DIM arr(10) AS INTEGER
DIM low AS INTEGER
DIM x AS INTEGER
CLS
x = 0
DO UNTIL x >= 10
INPUT "Enter Number: ", arr(x)
x = x + 1
LOOP
low = arr(1)
DO UNTIL x >= 11
IF arr(x) < low THEN
low = arr(x)
END IF
LOOP
CLS
PRINT "All Marks Are:"
PRINT arr(x); " ";
PRINT
PRINT "The Lowest Number is: "; low
我期待以下结果:
All Marks Are:
54 32 59 43 90 43 12 4 54 35
The Lowest Number is: 4
好的,我会在你的代码中改变一些东西。首先,
DO UNTIL x >= 10
INPUT "Enter Number: ", arr(x)
x = x + 1
LOOP
为什么在这里使用DO ... LOOP?我选择FOR循环:
FOR x = 1 to 10
INPUT "Enter Number: ", arr(x)
NEXT
它更传统,更短的代码,整体更清洁。
其次,你的第二个DO ... LOOP无法退出。是的,当然,当x大于或等于11时,它会退出,但是什么时候会发生?在你的循环中没有重新定义x,所以你的循环要么是无限的(如果x开始小于11),要么没有意义(如果x已经是11或更大)。在这种情况下,x此时将等于10,因此您的代码将如您所述冻结。
不幸的是,你尝试做的解析在QBasic中过于复杂,但它是可能的。为清楚起见,在程序的顶部定义TRUE
和FALSE
:
CONST TRUE = 1
CONST FALSE = 0
然后,当您到达要解析最小值的位置时,请执行以下操作:
finished% = TRUE 'I start by defining as TRUE and define as FALSE if I find
'a value which is smaller than the currently tested value.
CurTest% = 1 'This represents the array element which I am currently testing.
'It will change only when the program finds a value smaller than
'the one it is currently testing.
DO
finished% = TRUE
FOR i = CurTest% + 1 TO 10
IF arr(i) < arr(CurTest%) THEN
finished% = FALSE
CurTest% = i
EXIT FOR
END IF
NEXT i
LOOP UNTIL finished% = TRUE
'The loop will only complete once it has gone through a complete FOR...NEXT
'without finding a smaller value.
PRINT "The smallest value is:"; arr(CurTest%)
* N.B。:代码未经测试;可能有怪癖/错误。
希望这可以帮助!
稍微修改您的代码以获得低分数会导致:
DIM arr(10) AS INTEGER
DIM low AS INTEGER
DIM x AS INTEGER
CLS
x = 1
DO UNTIL x > 10
INPUT "Enter Number: ", arr(x)
x = x + 1
LOOP
low = arr(1)
x = 1
DO UNTIL x > 10
IF arr(x) < low THEN
low = arr(x)
END IF
x = x + 1
LOOP
CLS
PRINT "All Marks Are:"
FOR x = 1 TO 10
PRINT arr(x); " ";
NEXT
PRINT
PRINT "The Lowest Number is: "; low
您可能希望在第二个循环之前将x
设置回0(或使用不同的变量)。因为它会继续从第一个循环中断的地方增加x
。
它也可能遇到第二个循环的问题,因为你的数组只需要10个整数,但是你试图访问数组中的第11个位置。
更有效的方法来确定10个项目的最低分数:
REM determine lowest score of 10 items
CLS
FOR x = 1 TO 10
PRINT "Enter Number"; x;: INPUT arr(x)
IF x = 1 THEN low = arr(x)
IF arr(x) < low THEN
low = arr(x)
END IF
NEXT
PRINT "All Marks Are:"
FOR x = 1 TO 10
PRINT arr(x); " ";
NEXT
PRINT
PRINT "The Lowest Number is: "; low
确定任意数量项目的最高/最低/平均得分的另一个样本:
REM determine highest/lowest/average score of any number of items
CLS
PRINT "Number of items";: INPUT n
DIM arr(n) AS SINGLE
FOR x = 1 TO n
PRINT "Enter Number"; x;: INPUT arr(x)
IF x = 1 THEN low = arr(x): high = arr(x)
avg = avg + arr(x)
IF arr(x) < low THEN
low = arr(x)
END IF
IF arr(x) > high THEN
high = arr(x)
END IF
NEXT
PRINT "All Marks Are:"
FOR x = 1 TO n
PRINT arr(x);
NEXT
PRINT
PRINT "The Highest Number is:"; high
PRINT "The Lowest Number is:"; low
PRINT "The Average of all Scores is:"; avg / n
以上是关于使用DO UNTIL LOOP从阵列打印最小数字的主要内容,如果未能解决你的问题,请参考以下文章
C++中,do while, for loop和while loop之间有啥不同
VB中 while .....wend 与 DO while.....loop区别
VB中的do while……loop 和do……loop while语句是一样的吗