而在PL / SQL中循环和If / Else

时间:2017-10-10 07:56:33

标签: sql if-statement plsql while-loop oracle10g

我正在尝试编写一个会生成以下输出的过程

exec WinOrLose(4)

Welcome to the Win or Lose Game. Your number is 4. 

You win.
You lose.
You win.
You lose.

==> You lose!

到目前为止,我有这个:

CREATE or REPLACE Procedure WinOrLose (
  p_choice number ) AS

  v_answer number;

DECLARE
  v_answer := p_choice

BEGIN
  dbms_output.put_line ('Welcome to the Win or Lose Game. Your number is ' || 
  v_answer);

  FOR v_answer in 1..10 
    IF MOD(v_answer, 2) = 0 THEN -- v_answer is even
          dbms_output.put_line (You lose)

END;
/

我不确定从那里去哪里。我的思考过程(伪代码)是这样的:

SET v_answer := 1
   While Loop (outside)
     MOD(v_answer,2) = 0 then dbms.output (YOU LOSE)
   ELSE
     dbms.output (YOU WIN)
   end if;
     v_answer := p_choice

1 个答案:

答案 0 :(得分:2)

CREATE or REPLACE Procedure WinOrLose (
  p_choice number ) AS
BEGIN
  dbms_output.put_line ('Welcome to the Win or Lose Game. Your number is ' || 
  p_choice);

  FOR v_counter in 1..p_choice LOOP
    IF (MOD(v_counter, 2) = 0) 
    THEN
          dbms_output.put_line ('You win');
    ELSE
          dbms_output.put_line ('You lose');
    END IF;
  END LOOP;

  IF (MOD(p_choice , 2) = 0) 
  THEN
    dbms_output.put_line ('==> You win!');
  ELSE
    dbms_output.put_line ('==> You lose!');
  END IF;
END;
/