我希望我的程序输出输入的数组然后显示我输入的总量。总值是错误的,并且我的程序中的数组值发生了变化。我认为我的问题是添加部分。我该如何解决?
IDENTIFICATION DIVISION.
PROGRAM-ID. FOREXCHANGE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CURR-VAL.
02 USD PIC 999V99 VALUE 46.59.
02 AUS PIC 999V99 VALUE 32.87.
02 CND PIC 999V99 VALUE 36.37.
02 EUR PIC 999V99 VALUE 52.32.
02 YEN PIC 999V99 VALUE 0.40.
02 NZD PIC 999V99 VALUE 30.04.
02 OMN PIC 999V99 VALUE 120.99.
02 SGD PIC 999V99 VALUE 32.52.
77 AMOUNT PIC 9(6)V99.
77 NEWVAL PIC 9(4)V99.
77 ROUNDOFF PIC ZZZ,ZZ9.99.
77 VALCUR PIC 9(6)V99.
77 FORCUR PIC X(3).
77 TRANSAC PIC 9(4) OCCURS 9999 TIMES.
77 CURRENCIES PIC X(3) OCCURS 9999 TIMES.
77 AMOUNTARR PIC 9(6)V99 OCCURS 9999 TIMES.
77 AMOUNTTOT PIC 9(6)V99 OCCURS 9999 TIMES.
77 TRANSACNUM PIC 9(4) VALUE 0001.
77 CTR PIC 9(4) VALUE ZERO.
77 CTR1 PIC 9(4) VALUE ZERO.
77 CURINDEX PIC 9 VALUE 0.
77 CHOICE PIC X.
PROCEDURE DIVISION.
MAIN-MENU.
DISPLAY ERASE.
DISPLAY "MAIN MENU".
DISPLAY "1. TRANSACTION".
DISPLAY "2. UPDATE".
DISPLAY "3. SUMMARY".
DISPLAY "4. EXIT".
DISPLAY "ENTER CHOICE: " WITH NO ADVANCING.
ACCEPT CHOICE.
IF CHOICE = "1"
PERFORM ADDTRANS
ELSE IF CHOICE = "2"
PERFORM UPDATES
ELSE IF CHOICE = "3"
PERFORM SUMMARIES
ELSE IF CHOICE = "4"
STOP RUN
ELSE
DISPLAY "INVALID CHOICE! PLEASE TRY AGAIN!"
PERFORM MAIN-MENU.
SUMMARIES.
DISPLAY ERASE.
DISPLAY "SUMMARY".
DISPLAY "TRANSACTION NUMBER CURRENCY AMOUNT".
PERFORM WITH TEST AFTER VARYING CTR FROM 1 BY 1
UNTIL CTR = CURINDEX
DISPLAY " " TRANSAC(CTR) WITH NO ADVANCING
DISPLAY " " WITH NO ADVANCING
DISPLAY CURRENCIES(CTR)WITH NO ADVANCING
MOVE AMOUNTARR(CTR) TO ROUNDOFF
DISPLAY " " ROUNDOFF
END-PERFORM.
PERFORM WITH TEST AFTER VARYING CTR FROM 1 BY 1
UNTIL CTR = CURINDEX
ADD AMOUNTARR(CTR) TO AMOUNTARR(CTR)
END-PERFORM.
MOVE AMOUNTARR(CURINDEX) TO ROUNDOFF.
DISPLAY "TOTAL PHP" ROUNDOFF.
DISPLAY "GO BACK TO MAIN MENU?:[Y/N] "
WITH NO ADVANCING.
ACCEPT CHOICE.
IF CHOICE = "Y"
PERFORM MAIN-MENU
ELSE IF CHOICE = "N"
PERFORM SUMMARIES
ELSE
DISPLAY "INVALID CHOICE! GOING BACK TO MAIN MENU"
PERFORM MAIN-MENU.
ADDTRANS.
DISPLAY ERASE.
ADD 1 TO CURINDEX.
DISPLAY "TRANSACTION NUMBER: " TRANSACNUM.
MOVE TRANSACNUM TO TRANSAC(CURINDEX).
ADD 1 TO TRANSACNUM.
DISPLAY "ENTER FOREIGN CURRENCY: " WITH NO ADVANCING.
ACCEPT FORCUR.
IF FORCUR = "USD"
MOVE USD TO VALCUR
DISPLAY "ENTER AMOUNT: $" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE IF FORCUR = "AUS"
MOVE AUS TO VALCUR
DISPLAY "ENTER AMOUNT: $" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE IF FORCUR = "CND"
MOVE CND TO VALCUR
DISPLAY "ENTER AMOUNT: $" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE IF FORCUR = "EUR"
MOVE EUR TO VALCUR
DISPLAY "ENTER AMOUNT: E" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE IF FORCUR = "YEN"
MOVE YEN TO VALCUR
DISPLAY "ENTER AMOUNT: Y" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE IF FORCUR = "NZD"
MOVE NZD TO VALCUR
DISPLAY "ENTER AMOUNT: $" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE IF FORCUR = "OMN"
MOVE OMN TO VALCUR
DISPLAY "ENTER AMOUNT: R" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE IF FORCUR = "SGD"
MOVE SGD TO VALCUR
DISPLAY "ENTER AMOUNT: $" WITH NO ADVANCING
ACCEPT AMOUNT
ELSE
DISPLAY "INVALID FOREIGN CURRENCY! PLEASE TRY AGAIN!"
SUBTRACT 1 FROM CURINDEX
SUBTRACT 1 FROM TRANSACNUM
PERFORM MAIN-MENU.
MOVE FORCUR TO CURRENCIES(CURINDEX).
MULTIPLY AMOUNT BY VALCUR GIVING AMOUNTARR(CURINDEX).
MOVE AMOUNTARR(CURINDEX) TO ROUNDOFF.
DISPLAY "PHP " ROUNDOFF.
DISPLAY "ENTER ANOTHER TRANSACTION?:[Y/N] "
WITH NO ADVANCING.
ACCEPT CHOICE.
IF CHOICE = "Y"
PERFORM ADDTRANS
ELSE IF CHOICE = "N"
PERFORM MAIN-MENU
ELSE
DISPLAY "INVALID CHOICE! GOING BACK TO MAIN MENU"
PERFORM MAIN-MENU.
UPDATES.
DISPLAY ERASE.
DISPLAY "ENTER FOREIGN CURRENCY: " WITH NO ADVANCING.
ACCEPT FORCUR.
IF FORCUR = "USD"
MOVE USD TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: $" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO USD
ELSE IF FORCUR = "AUS"
MOVE AUS TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: $" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO AUS
ELSE IF FORCUR = "CND"
MOVE CND TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: $" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO CND
ELSE IF FORCUR = "EUR"
MOVE EUR TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: E" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO EUR
ELSE IF FORCUR = "YEN"
MOVE YEN TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: Y" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO YEN
ELSE IF FORCUR = "NZD"
MOVE NZD TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: $" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO NZD
ELSE IF FORCUR = "OMN"
MOVE OMN TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: R" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO OMN
ELSE IF FORCUR = "SGD"
MOVE SGD TO ROUNDOFF
DISPLAY "RECENT VALUE: " ROUNDOFF
DISPLAY "RATE: $" WITH NO ADVANCING
ACCEPT NEWVAL
MOVE NEWVAL TO SGD
ELSE
DISPLAY "INVALID FOREIGN CURRENCY! PLEASE TRY AGAIN!"
PERFORM MAIN-MENU.
DISPLAY "--GO BACK TO MAIN MENU".
PERFORM MAIN-MENU.
更新: 我设法修复了数组中值的更改,但添加仍然是错误的。我发现了问题。数组中的最后一个值会自行添加。
SUMMARIES.
DISPLAY ERASE.
DISPLAY "SUMMARY".
DISPLAY "TRANSACTION NUMBER CURRENCY AMOUNT".
PERFORM WITH TEST AFTER VARYING CTR FROM 1 BY 1
UNTIL CTR = CURINDEX
DISPLAY " " TRANSAC(CTR) WITH NO ADVANCING
DISPLAY " " WITH NO ADVANCING
DISPLAY CURRENCIES(CTR)WITH NO ADVANCING
MOVE AMOUNTARR(CTR) TO ROUNDOFF
DISPLAY " " ROUNDOFF
END-PERFORM.
PERFORM WITH TEST AFTER VARYING CTR1 FROM 1 BY 1
UNTIL CTR1 = CURINDEX
MOVE AMOUNTARR(CTR1) TO AMOUNTTOT(CTR1)
ADD AMOUNTTOT(CTR1) TO AMOUNTTOT(CTR1)
END-PERFORM.
答案 0 :(得分:3)
您无法使用PERFORM如何使用它。 PERFORM不能以任何方式递归。
即使它可以,这将是" spaghetti"代码:除非您知道当时的来源,否则您无法告诉您在该计划中的位置。你从一条曲折的蠕动路线走到另一条路。
另一个问题是你正在经历"到处都是。如果您从紧接其前一行的段落到达,则段落将执行。这解释了你显然是随机添加的内容。
程序中的代码可能就在那里,但您需要完全重构它。如果您在此处查看COBOL问题的一些答案,您将会看到一些示例代码。
这应该类似于你的控制结构:
ACCEPT CHOICE
PERFORM UNTIL CHOICE EQUAL TO "4"
IF CHOICE = "1"
PERFORM ADDTRANS
ELSE IF CHOICE = "2"
PERFORM UPDATES
ELSE IF CHOICE = "3"
PERFORM SUMMARIES
ELSE
DISPLAY "INVALID CHOICE! PLEASE TRY AGAIN!"
END-IF
END-IF
END-IF
ACCEPT CHOICE
END-PERFORM
您应该将nested-IF
更改为EVALUATE
,并且您应该将字段值的测试更改为字段COBOL&{39} 88
上的condition-names
。
您必须重组您的计划。目前,它不可行,不可靠,不便携,没有任何用处。但是,只有结构(可能)是错误的。如果你掌握了如何构建一个程序,以及段落如何落实/去除(除非你没有看到一种不通过的方式,否则不要这样做)总有一种方法不会/ /表现。
最近有一个问题,答案是"堕落"。请阅读:https://stackoverflow.com/a/32885852/1927206
请记住,虽然结构错误,但代码是可以挽救的。这是COBOL初学者常犯的错误,所以不是大问题。有益于学习。试着看看这里的例子。