--1 判断输入的数是不是水仙花数declare
n int:='&请输入一个数:';
a int;
b int;
c int;beginif n <100or n >=1000then
dbms_output.put_line(n||'不是三位数,肯定就不是水仙花数!');return;endif;-- 获取给个位数的值
a:=mod(n,10);
b:=mod(trunc(n/10,0),10);
c:=trunc(n/100,0);-- 判断各个位数的三次方之和 是否等于次数if a*a*a+b*b*b+c*c*c=n then
dbms_output.put_line(n||'是水仙花数!');else
dbms_output.put_line(n||'是三位数,但不是水仙花数!');endif;
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;
--2 输入年月日 求此时间距离今天的天数declare
y int:='&请输入一个年:';
m int:='&请输入一个月:';
d int:='&请输入一个日:';
days int;
date1 date;begin
date1:=to_date(y||'-'||m||'-'||d,'yyyy-mm-dd');
days:=sysdate-date1;
dbms_output.put_line(date1||'距离今天有'||days||'天!');
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;
-- 2.1 判断一个年 是不是闰年declare
y int:='&请输入一个年:';beginifmod(y,400)=0then
dbms_output.put_line(y||'是闰年!');
elsif mod(y,4)=0andmod(y,100)!=0then
dbms_output.put_line(y||'是闰年!');else
dbms_output.put_line(y||'不是闰年!');endif;
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;-- 条件合并declare
y int:='&请输入一个年:';beginifmod(y,400)=0or(mod(y,4)=0andmod(y,100)!=0)then
dbms_output.put_line(y||'是闰年!');else
dbms_output.put_line(y||'不是闰年!');endif;
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;
-- 2.2 判断指定年 指定月 有多少天declare
y int:='&请输入一个年:';
m int:='&请输入一个月:';
days int;begincase m
when4then
days:=30;when6then
days:=30;when9then
days:=30;when11then
days:=30;when2thenifmod(y,400)=0or(mod(y,4)=0andmod(y,100)!=0)then
days:=29;else
days:=28;endif;else
days:=31;endcase;
dbms_output.put_line(y||'年'||m||'月有'||days||'天!');
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;
--3 查询student表某个学生的年龄和性别 判断称呼:8个称呼 altertable stuinfo add sex char(3);update stuinfo set sex='女';update stuinfo set sex='男'wheremod(sno,2)=0;select*from stuinfo;declare
sn int:='&请输入一个学号:';
sx char(3);
sg int;
changHu varchar(20);beginselect sex,sage into sx,sg from stuinfo where sno=sn;if sx ='男'thenif sg <18then
changHu:='小男孩';
elsif sg <35then
changHu:='帅哥';
elsif sg <60then
changHu:='叔叔';else
changHu:='爷爷';endif;elseif sg <18then
changHu:='小女孩';
elsif sg <35then
changHu:='美女';
elsif sg <60then
changHu:='阿姨';else
changHu:='奶奶';endif;endif;
dbms_output.put_line(sx||':'||sg||':::称呼是:'||changHu);
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;
/*
循环结构:loop while for
循环结构之loop: loop格式
loop
循环执行的代码;
if 条件 then
exit;
end if;
end loop;
*/-- 打印1到100的和declare
sumn int:=0;
n int:=1;beginloop
sumn:=sumn+n;
n:=n+1;if n >100thenexit;endif;endloop;
dbms_output.put_line('1到100的和:'||sumn);
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;
-- 99乘法表declare
i int:=1;
j int:=1;beginloop
j:=1;loop
dbms_output.put(i||'*'||j||'='||i*j||' ');
j:=j+1;if j > i thenexit;endif;endloop;
i:=i+1;if i >9thenexit;endif;
dbms_output.put_line('');endloop;
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;
/*
循环结构之while: while格式
while 条件 loop
循环执行的代码;
end loop;
*/-- 99乘法表declare
i int:=1;
j int:=1;beginwhile i<=9loop
j:=1;while j<=i loop
dbms_output.put(i||'*'||j||'='||i*j||' ');
j:=j+1;endloop;
i:=i+1;
dbms_output.put_line('');endloop;
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;-- 判断一个数是不是质数declare
n int:='&请输入一个数:';
m int:=2;
b boolean:=true;beginwhile m < n loopifmod(n,m)=0then
b:=false;exit;endif;
m:=m+1;endloop;if b=trueand n >1then
dbms_output.put_line(n||'是质数!');endif;
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;/*
循环结构之for: for格式
for n in start..end loop
循环执行的代码;
end loop;
*/-- 获取1到100的和declare
sumn int:=0;beginfor n in1..100loop
sumn:=sumn+n;endloop;
dbms_output.put_line('1到100的和:'||sumn);
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;-- 99乘法表beginfor i in1..9loopfor j in1..i loop
dbms_output.put(i||'*'||j||'='||i*j||' ');endloop;
dbms_output.put_line('');endloop;
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;-- 获取一个字符串 数字字符 字母字符 和其他字符的次数declare
str varchar(100):='&请输入一个字符串:';
sz int:=0;
zm int:=0;
qt int:=0;
c char(3);beginfor n in1..length(str)loop-- dbms_output.put_line(n||'::'||substr(str,n,1));-- 获取n位置处的字符
c:=substr(str,n,1);if ascii(c)>=ascii('0')and ascii(c)<=ascii('9')then
sz:=sz+1;
elsif ascii(c)>=ascii('a')and ascii(c)<=ascii('z')then
zm:=zm+1;
elsif ascii(c)>=ascii('A')and ascii(c)<=ascii('Z')then
zm:=zm+1;else
qt:=qt+1;endif;endloop;
dbms_output.put_line(str||'中有'||sz||'个数字,'||zm||'个字母,'||qt||'个其他字符!');
exception
when others then
dbms_output.put_line('出错了!!!:::::');end;