2011-01-30 91 views
2
SQL> ed 
Wrote file afiedt.buf 

    1 declare 
    2 n number; 
    3 i number; 
    4 counter number; 
    5 begin 
    6 n:=&n; 
    7 i:=1; 
    8 counter:=0; 
    9 if n=1 
10  then dbms_output.put_line('1 is a prime No.'); 
11 else if n=2 
12  then dbms_output.put_line('2 is even prime'); 
13 else 
14  for i in 1..n loop 
15  if mod(n,i)=0 
16    then counter:=counter+1; 
17    end if; 
18  end loop; 
19 end if; 
20 if counter=2 
21  then dbms_output.put_line(n||' is a prime No.'); 
22 else 
23  dbms_output.put_line(n||' is a not prime No.'); 
24 end if; 
25* end 

我收到以下错误,我不明白。任何人都可以解释是什么造成的?素数码 - 请帮我解决这个错误('如果丢失'?)

SQL>/
Enter value for n: 8 
old 6: n:=&n; 
new 6: n:=8; 
end 
    * 
ERROR at line 25: 
ORA-06550: line 25, column 3: 
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the 
following: 
if 
+0

请解释一下你的问题 - 你想要你的代码做什么以及它做什么? – Mikaveli 2011-01-30 10:58:28

+0

注:1通常不被视为素数。请参阅:http://mathworld.wolfram.com/PrimeNumber.html。 “一个素数......是一个正整数p> 1,除1和它自身之外没有正整数除数。” “数字1是一个既不是素数又不是复合数的特殊情况(Wells 1986,p.31)尽管数字1曾被认为是素数(Goldbach 1742; Lehmer 1909,1914; Hardy and Wright 1979,p 11; Gardner 1984,pp.86-87; Sloane和Plouffe 1995,第33页; Hardy 1999,第46页)。 – 2011-01-31 20:14:46

回答

7

这是因为该代码段,格式给你一个更好的understnding的原因:

9 if n=1 
10 | then dbms_output.put_line('1 is a prime No.'); 
11 else 
    | if n=2 
12 | | then dbms_output.put_line('2 is even prime'); 
13 | else 
14 | | for i in 1..n loop 
15 | | | if mod(n,i)=0 
16 | | | | then counter:=counter+1; 
17 | | | end if; 
18 | | end loop; 
19 | end if; 
    ? 

换句话说,这部分缺失的end if。这就是为什么当你在第25行用end完成你的文件时,它会抱怨它没有if(制作end if)。

只要按照第19行紧接着一个end if并且应该修复它。


这应该可以解决您的直接问题,但我也有一些快速评论。

  • 如果使用for i in 2..n loop(在2,而不是1开始),以及测试counter是(而不是等于2)大于1,这应该节省一些工作。 mod(n,1)为零为全部n
  • 在任何情况下,原始测试应该是大于大于2,不等于:数字12会给你一个counter为5(1,2,3,4和6分别为1)等等将被视为非素数。
  • 为了提高工作效率,您应该记住,您只需检查trunc(sqrt(n))+1(或您使用的任何语言的等效语言),因为如果高于此数字的因数,您就会发现它的对已存在:12 mod 4为零,但你已经发现它的3对(12 mod(12/4)为零)。
  • 确保该2..n不包括n因为mod操作也会有计数器加一(我不知道你的具体的语言如何处理循环结构) - 它可能必须2..n-1
+0

有帮助,但最终会错过报告的语法错误,这是25行缺失的分号。如果错误得到纠正,那么报告的错误就会被报告。另外,“无论如何,原始测试应该大于2,不等于...”是不正确的。由于循环为1..N`mod(n,i)= 0`对素数发生了两次,一次出现`i = 1`,一次出现`i = N`。当然,循环缩短到N的平方根,最终的测试将不得不改变。 – 2011-01-31 20:29:08

4

我认为您正在寻找关键字PL/SQL ELSIF。我把你的代码,以

elsif n=2 

更换

​​ 上线11

和它的工作。

1

1)你必须在25行2)然后你会得到另一个错误,最终end后分号(参见下面的注释1),并需要通过线11 3)Finaly代elsifelse if与纠正, 1不是质数,10号线需要修正也then dbms_output.put_line('1 is neither prime nor composite.);所以更正后的代码是:

SQL> declare 
    2  n number; 
    3  i number; 
    4  counter number; 
    5 begin 
    6  n:=&n; 
    7  i:=1; 
    8  counter:=0; 
    9  if n=1 
10   then dbms_output.put_line('1 is neither prime nor composite.'); 
11  elsif n=2 
12   then dbms_output.put_line('2 is even prime'); 
13  else 
14   for i in 1..n loop 
15    if mod(n,i)=0 
16     then counter:=counter+1; 
17    end if; 
18   end loop; 
19  end if; 
20  if counter=2 
21   then dbms_output.put_line(n||' is a prime No.'); 
22  else 
23   dbms_output.put_line(n||' is a not prime No.'); 
24  end if; 
25 end; 
26/
Enter value for n: 3 
old 6:  n:=&n; 
new 6:  n:=3; 
3 is a prime No. 

PL/SQL procedure successfully completed. 

而且,看到这提高代码关于素数paxdiablo's答案补充说明。


注1:第二个语法错误的样子:

SQL> declare 
    2  n number; 
    3  i number; 
    4  counter number; 
    5 begin 
    6  n:=&n; 
    7  i:=1; 
    8  counter:=0; 
    9  if n=1 
10   then dbms_output.put_line('1 is a prime No.'); 
11  else if n=2 
12   then dbms_output.put_line('2 is even prime'); 
13  else 
14   for i in 1..n loop 
15    if mod(n,i)=0 
16     then counter:=counter+1; 
17    end if; 
18   end loop; 
19  end if; 
20  if counter=2 
21   then dbms_output.put_line(n||' is a prime No.'); 
22  else 
23   dbms_output.put_line(n||' is a not prime No.'); 
24  end if; 
25 end; 
26/
Enter value for n: 10 
old 6:  n:=&n; 
new 6:  n:=10; 
end; 
    * 
ERROR at line 25: 
ORA-06550: line 25, column 4: 
PLS-00103: Encountered the symbol ";" when expecting one of the following: 
if 
2

IF我们应该利用END IF;

关闭,但是当我们使用ELSEIF一个就够了。

IF 
    ...... 
    ELSEIF 
    ...... 
END IF; 

IF 
    ...... 
    ELSE IF 
    ....... 
    END IF; 
END IF; 
0
DECLARE 
    N NUMBER:=&N; 
    V_COUNT NUMBER:=0; 
BEGIN 
    FOR I IN 1..N LOOP 
    IF MOD(N,I)=0 THEN 
     V_COUNT:=V_COUNT+1; 
    END IF; 
    END LOOP; 
    IF V_COUNT<=2 THEN 
    DBMS_OUTPUT.PUT_LINE(N||' ' ||'IS PRIME NUMBER'); 
    ELSE 
    DBMS_OUTPUT.PUT_LINE(N||' '||' IS NOT PRIME NUMBER'); 
    END IF; 
END; 
0
declare 
    n  number; 
    i  number; 
    counter number; 
begin 
    n  := &n; 
    i  := 1; 
    counter := 0; 
    if n = 1 then 
    dbms_output.put_line('1 is a prime No.'); 
    elsif n = 2 then 
    dbms_output.put_line('2 is even prime'); 
    else 
    for i in 1 .. n loop 
     if mod(n, i) = 0 then 
     counter := counter + 1; 
     end if; 
    end loop; 
    end if; 

    if counter = 2 then 
    dbms_output.put_line(n || ' is a prime No.'); 
    else 
    dbms_output.put_line(n || ' is a not prime No.'); 
    end if; 

end; 
0

质数检查。

DECLARE 
    N NUMBER :=&N; 
    I NUMBER; 
    COUNTER NUMBER :=0; 
BEGIN 
    FOR I IN 1..N LOOP 
    IF(MOD(N,I) =0) THEN 
    COUNTER :=COUNTER+1; 
END IF; 
END LOOP; 
IF (COUNTER =2) THEN 
DBMS_OUTPUT.PUT_LINE(N ||' '||'IS PRIME NUMBER'); 
ELSE 
DBMS_OUTPUT.PUT_LINE(N ||' '||'IS NOT A PRIME NUMBER'); 
END IF; 
END;