ABAP - 乘法代码错误

时间:2014-07-09 00:35:38

标签: sap abap

这是我第一次参加ABAP。


DATA: n(1) TYPE I VALUE '2',
      sum(2) TYPE I.

DEFINE multiple.
   WHILE sy-index < 10.
     sum = &1 * sy-index.
     WRITE: / &1, 'x', sy-index,  sum.
   ENDWHILE.
END-OF-DEFINITION.

DO 9 TIMES.
   IF sy-index = 1.
     CONTINUE.
   ENDIF.

   multiple sy-index.
ENDDO.

所以,低于我的程序产生的输出。

1 x 1    1
2 x 2    4
3 x 3    9

实际结果不是我所期待的。


预期结果应该是......

2 x 1  2
2 x 2  4
2 x 3  6
2 x 4  8
2 x 5  10

..
..
..

3 个答案:

答案 0 :(得分:1)

首先,请不要对表单,宏,方法或功能模块中的全局变量进行操作。将其作为参数传递。

其次,这是解决问题的方法。

我还将multiple功能保留为宏,但应该至少实现为FORM

DATA: sum(2) TYPE i.
DATA: l_outer_loop_index TYPE i.

DEFINE multiple.
  sum = &1 * &2.
  WRITE: / &1, 'x', &2,  sum.
END-OF-DEFINITION.

DO 9 TIMES.
   IF sy-index = 1.
     CONTINUE.
   ENDIF.
   l_outer_loop_index = sy-index.
   DO 9 TIMES.
     multiple l_outer_loop_index sy-index.
   ENDDO.
ENDDO.

答案 1 :(得分:0)

感谢贾格尔,我引用他的答案。我有一个垃圾变化。这是我的第一个答案。我希望这可以帮到你。

DATA: sum(2) TYPE i.
DATA: sumstring(2) TYPE c.
DATA: l_outer_loop_index TYPE i.
DATA: result(100) TYPE c.
DATA: num1(10) TYPE c,
      num2(10) TYPE c.
DEFINE multiple.
  clear: result.
  sum = &1 * &2.
  WRITE &1 to num1.
  WRITE &2 to num2.
  WRITE sum to sumstring.
  CONDENSE sumstring.
  CONDENSE num1.
  CONDENSE num2.
  concatenate  num1 'x' num2 '=' sumstring INTO result SEPARATED BY space.
  WRITE: / result.
END-OF-DEFINITION.
DO 9 TIMES.
  IF sy-index = 1.
    CONTINUE.
  ENDIF.
  l_outer_loop_index = sy-index.
  DO l_outer_loop_index TIMES.
    multiple l_outer_loop_index sy-index.
    IF l_outer_loop_index = sy-index.
       WRITE: / .
    ENDIF.
  ENDDO.
ENDDO.

答案 2 :(得分:-1)

尝试我的解决方案

DATA: n(1)   TYPE i VALUE '2',
      sum(2) TYPE i.


DO 9 TIMES.
   IF sy-index = 1.
     CONTINUE.
   ENDIF.

   PERFORM multi using sy-index.
ENDDO.

FORM multi USING i_num TYPE i.
  DATA: lv_num TYPE i.

  MOVE i_num TO lv_num.
     DO 10 TIMES.
      IF sy-index = 1.
        CONTINUE.
      ENDIF.
     sum = lv_num * sy-index.

     WRITE: / lv_num, 'x', sy-index,  sum.

   ENDDO.
ENDFORM.