码迷,mamicode.com
首页 > Web开发 > 详细

jsp上机实验-斐波那契数列的表格输出 横向表格输出

时间:2018-09-27 01:54:39      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:out   文本   字符   完成   使用数组   数据   ring   table   ble   

1、在页面中计算并以文本的方式输出100以内的Fibonacci数列:1、1、2、3、5、8、13、21、34、55、89。

要求:不使用数组,定义三个变量a, a1 和a2,再定义一个变量n用来计数。

 

    <%
        int a=1,a1=1,a2=1;
        out.println(a);
        while(a2<100){
            out.println(a2);
            a2= a1+a;
            a= a1;
            a1 = a2;    
        }
    %>

 

2、使用竖向表格的方式,完成数据的输出,输出形式如下

技术分享图片

<p>试问菲波那切数列从第几项开始大于100:</p>
<table border=1 >
<tr>
<th>项序号</th>
<th>值</th>
</tr>
<%
        int a=1,a1=1,a2=1,n=1;
        out.println("<tr>"+"<th>"+n+"</th>");
        out.println("<th>"+a+"</th>"+"</tr>");
        while(a2<100){
            n++;
            out.println("<tr>"+"<th>"+n+"</th>");
            out.println("<th>"+a2+"</th>"+"</tr>");
            a2= a1+a;
            a= a1;
            a1 = a2;    
        }
    %>
</table>
<p>菲波那切数列从第<%=n %>项开始大于100</p>

 

3、将数据以横向表格的方式输出,上面一行为“项序号”,下面一行为“值”。如下图

技术分享图片

1、思路一:利用两个字符串

<p>试问菲波那切数列从第几项开始大于100:</p>
<table  >

<%!
    String str1="<th>项序号</th>",str2="<th>值</th>";
%>
<%
        int a=1,a1=1,a2=1,n=1;
        str1+="<td>"+n+"</td>";
        str2+="<td>"+a+"</td>";
        
        
        while(a2<100){
            n++;
            str1+="<td>"+n+"</td>";
            str2+="<td>"+a2+"</td>";
            a2= a1+a;
            a= a1;
            a1 = a2;    
        }
        out.print("<tr>"+str1);
        out.print("<tr>"+str2);
    %>
</table>
<p>菲波那切数列从第<%=n %>项开始大于100</p>

 

2、思路二:表格嵌套(较为繁琐)

<p>试问菲波那切数列从第几项开始大于100:</p>
<table>
<th>
<table border=1>
    <tr>
    <td>项序号</td>
    </tr>
    <tr>
    <td>值</td>
    </tr>
</table>
</th>
<%
        int a=1,a1=1,a2=1,n=1;
        out.print("<th><table border=1><tr><td>"+n+"</td></tr><tr><td>"+a+"</td></tr></table></th>");
        
        
        while(a2<100){
            n++;
            out.print("<th><table border=1><tr><td>"+n+"</td></tr><tr><td>"+a2+"</td></tr></table></th>");
            a2= a1+a;
            a= a1;
            a1 = a2;    
        }
    %>
</table>
<p>菲波那切数列从第<%=n %>项开始大于100</p>

 

jsp上机实验-斐波那契数列的表格输出 横向表格输出

标签:out   文本   字符   完成   使用数组   数据   ring   table   ble   

原文地址:https://www.cnblogs.com/Gaohy/p/9710880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!