oracle 质数代码 - 请帮我解决这个错误('missing if'?)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4842480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prime number code - please help me in solving this error ('missing if'?)
提问by Sai Pratap
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
I get the following error which I don't understand. Can anyone explain what's causing it?
我收到以下我不明白的错误。谁能解释是什么原因造成的?
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
回答by paxdiablo
It's because of this code segment, formatted to give you a better understnding why:
这是因为这个代码段,格式化让你更好地理解为什么:
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;
?
In other words, that section is missing an end if
. That's why, when you finish your file on line 25 with end
, it complains about not having an if
after it (to make end if
).
换句话说,该部分缺少end if
. 这就是为什么,当您在第 25 行使用 完成文件时end
,它会抱怨没有后缀if
(make end if
)。
Just whack an end if
immediately following line 19 and that should fix it.
只需敲击end if
紧随其后的第 19 行就可以解决它。
That should fix your immediate problem but I have a couple of quick comments as well.
这应该可以解决您当前的问题,但我也有一些简短的评论。
- if you use
for i in 2..n loop
(start at 2 instead of 1), and testcounter
being greater than 1 (instead of equal to 2), that should save some work.mod(n,1)
is zero for alln
. - in any case, the original test should have been for greaterthan 2, not equal: the number 12 would have given you a
counter
of 5 (one each for 1, 2, 3, 4 and 6) and so would be considered non-prime. - for efficiency, you should remember that you only ever have to check up to
trunc(sqrt(n))+1
(or the equivalent in whatever language you're using) since, if a number higher than that is a factor, you would have found its pair already: 12 mod 4 is zero but you've already found its pair of 3 (12 mod (12/4) is zero). - make sure that the
2..n
does not includen
since themod
operation there will also increment counter (I don't know how your specific language handles that loop construct) - it mayhave to be2..n-1
.
- 如果您使用
for i in 2..n loop
(从 2 而不是 1 开始),并且测试counter
大于 1(而不是等于 2),那应该可以节省一些工作。mod(n,1)
对所有都是零n
。 - 在任何情况下,原始测试都应该大于2,而不是相等:数字 12 会给你一个
counter
5(1、2、3、4 和 6 各一个),因此将被视为非质数. - 为了效率,您应该记住,您只需要检查
trunc(sqrt(n))+1
(或您使用的任何语言中的等效项),因为如果一个数字高于该因素,您就会发现它的对:12 mod 4为零,但您已经发现它的一对 3(12 mod (12/4) 为零)。 - 确保
2..n
不包含,n
因为mod
那里的操作也会增加计数器(我不知道您的特定语言如何处理该循环构造) - 它可能必须是2..n-1
.
回答by Luke Woodward
I think you're looking for the PL/SQL ELSIF
keyword. I took your code, replaced
我认为您正在寻找 PL/SQLELSIF
关键字。我拿走了你的代码,替换了
else if n=2
on line 11 with
在第 11 行与
elsif n=2
and it worked.
它奏效了。
回答by KARTHIK
For every IF
we should close it by using END IF;
对于每一个IF
我们应该通过使用关闭它END IF;
But when we use ELSEIF
one is enough.
但是当我们使用ELSEIF
一个就足够了。
IF
......
ELSEIF
......
END IF;
or
或者
IF
......
ELSE IF
.......
END IF;
END IF;
回答by Shannon Severance
1) You need a semicolon after the final end
at line 25. 2) Then you get another error, (See note 1 below) and need to correct with by substituting elsif
for else if
at line 11. 3) Finaly, 1 not being prime, line 10 needs correction also, then dbms_output.put_line('1 is neither prime nor composite.);
So the corrected code is:
1)您需要的决赛后,一个分号end
在行25. 2)然后,通过代得到另一个错误,(下面见注1),并需要用正确elsif
的else if
在第11行3)Finaly,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.
Also, see paxdiablo'sanswer for additional notes on improving this code with respect to prime numbers.
另外,请参阅paxdiablo 的回答以获取有关改进此代码的质数的附加说明。
Note 1:The second syntax error looks like:
注 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
回答by SURESH REDDY
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;
回答by Kamal Goduguluri
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;
回答by Kishor Reddy Kalluri
prime number checking.
质数检查。
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;