如何在plsql中找到偶数的总和?

时间:2015-10-23 11:08:18

标签: oracle loops plsql modulo

declare
       n number(4);
       s number(4);
       i number(4);
     count number(4);

begin
       n:=&n;
       s:=0;
       count:=0;

       while (n>0 AND count MOD 2 = 0)
       loop
              i:= n mod 10;
              s:=s+i;
              n:=trunc(n / 10) ;  
              count:=count+1;  
       end loop;

       dbms_output.put_line('Sum of digit = ' || s);

end;

我已经尝试了但是收到了错误:

ERROR at line 12:
ORA-06550: line 12, column 23:
PLS-00204: function or pseudo-column 'COUNT' may be used inside a SQL statement
only
ORA-06550: line 12, column 8:
PL/SQL: Statement ignored

3 个答案:

答案 0 :(得分:0)

MOD is a function,而不是运营商。尝试:

Module Module1

Structure Car           
    Public carmake As String           
    Public carmodel As String         
    Public caryear As Integer       
    Public carlicence As String
End Structure

Sub Main()

Dim userchoice

Console.WriteLine("Choose weather to open the database(1), print it (2) or end (3)")

        userchoice = Console.ReadLine()
        If userchoice = "1" Then
            Dim cardatabase(4) As Car
            Console.WriteLine("This will allow you to view the database.")
            Console.WriteLine("Please enter the car make,licence,model and year")
            cardatabase(1).carmake = Console.ReadLine()
            cardatabase(1).carlicence = Console.ReadLine()
            cardatabase(1).carmodel = Console.ReadLine()
            cardatabase(1).caryear = Console.ReadLine()


        ElseIf userchoice = "2" Then
            Console.WriteLine("The database is,")
        ElseIf userchoice = "3" Then
            Console.WriteLine("Thanks for using this program.")
        End If
        Console.ReadLine()
    End Sub
End Module

答案 1 :(得分:0)

您必须重命名COUNT变量,因为它是Oracle中的保留字。

但我不确定你的逻辑是否合适。

第一步 - cnt=1, mod (cnt,2) = 1,你在循环中

第二步 - while,您不会进入循环内部,IF MOD(cnt,2) = 0 THEN ....将会结束。也许你应该做count

我将cnt重命名为foreach (int numImages in array) { for (int i = 1; i <= numImages; i++) { CreateImage(); } }

答案 2 :(得分:0)

计数应该是“计数”bcz它是中的保留字条件,而不正确。如果在n = 150以下的样本中,结果将为零

declare
           n number(4);
           s number(4);
           i number(4);
           "count" number(4);


begin
       n:=150;
       s:=0;
       "count":=0;

       while (n>0)
       loop
              i:= n mod 10;
              s:=s+i;
              n:=trunc(n / 10) ;  
              if ("count" MOD 2 = 0)
              then             
                "count":="count"+1;  
              end if;  
       end loop;

       dbms_output.put_line('Sum of digit = ' || s);

end;
相关问题