计算单利并以表格形式输出

时间:2019-04-14 12:00:24

标签: qbasic

我正在尝试编写QBASIC代码,允许用户输入5位投资者的主体时间来计算 单利< / strong> / 5个投资者输出 表格中的应计金额本金时间/持续时间。所有利率均为12%< / p>

我尝试:

DIM P AS DOUBLE
DIM r AS DOUBLE
DIM t AS DOUBLE
DIM A AS DOUBLE
DIM rt AS DOUBLE
CLS
DIM x AS INTEGER
CLS
WHILE x <> 5
    INPUT "Enter First Investor's Principal: ", P
    INPUT "Enter First Investor's Time: ", t
    x = x + 1

WEND
r = 0.12
rt = 1 + (r * t)
A = P * rt
FOR i = 1 TO x
    PRINT "Amount Accrued", "Principal Amount", "Time"
    PRINT
    PRINT A, , P, , t
NEXT i

但是它会在5个不同的表格中连续打印最后个结果5次

我期望的结果如下:

Amount Accrued    Principal   Time/Duration
44,400            30,000      4
68,000            50,000      3
95,200            70,000      5

2 个答案:

答案 0 :(得分:2)

之所以会这样,是因为您在P循环的每次迭代中都覆盖tWHILE。将Pt以及其他后续变量初始化为数组,并在它们的不同索引处存储不同的值。另请注意,每次迭代中始终打印 First Investor's Principal and Time。投资者人数也应该迭代。我已经修复了以下代码中的问题:

CLS
DIM x AS INTEGER
INPUT "Number of Investors: ", x
DIM P(x - 1) AS DOUBLE
DIM r AS DOUBLE
DIM t(x - 1) AS DOUBLE
DIM A(x - 1) AS DOUBLE
DIM rt(x - 1) AS DOUBLE

FOR i = 0 TO x - 1
    PRINT "Enter Investor-" + STR$(i + 1) + "'s Principal: "
    INPUT "", P(i)
    PRINT "Enter Investor-" + STR$(i + 1) + "'s Time: "
    INPUT "", t(i)
NEXT i

PRINT "Amount Accrued", "Principal Amount", "Time"
PRINT

r = .12
FOR i = 0 TO x - 1
    rt(i) = 1 + (r * t(i))
    A(i) = (P(i) * rt(i))
    PRINT A(i), P(i), , t(i)
NEXT i

答案 1 :(得分:1)

一个简单的应计利息编码示例:

注意:数组默认为10个项目,不需要DIM,变量默认为单个。

select