使用 DO UNTIL LOOP 打印数组中的最小数字

Printing The Lowest Number From An Array Using 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

您可能希望在第二次循环之前将 x 设置回 0(或使用不同的变量)。因为它会从您在第一个循环中停止的地方继续递增 x

它也可能 运行 在第二个循环中出现问题,因为你的数组只需要 10 个整数,但你试图访问数组中的第 11 个位置。

好的,我会更改您的代码中的一些内容。首先,

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 中过于复杂,但这是可能的。为了清楚起见,在程序的顶部定义 TRUEFALSE:

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

确定 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

确定任意数量项目的 highest/lowest/average 分数的另一个示例:

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