Java JSP中的计算问题

Java JSP中的计算问题,java,jsp,Java,Jsp,这个代码的问题是,我得到的一个作者的受欢迎度是0%,我的意思是,如果借书的数量是14本,而所选作者的借书总数是3本,那么这个数字应该是21.42%。为什么会这样 除最后一个结果外,所有结果均正确: 对于上述给定数据,作者的受欢迎程度为0% <% String requestedoprations = request.getParameter("popularity"); if("check".equalsIgnoreCase(requestedoprations)){ int nu

这个代码的问题是,我得到的一个作者的受欢迎度是0%,我的意思是,如果借书的数量是14本,而所选作者的借书总数是3本,那么这个数字应该是21.42%。为什么会这样

除最后一个结果外,所有结果均正确:

对于上述给定数据,作者的受欢迎程度为0%

<%
String requestedoprations = request.getParameter("popularity");
if("check".equalsIgnoreCase(requestedoprations)){
    int num=LimsHandler.getInstance().popularitycheck(
        request.getParameter("selectedauthor"));
    if(num!=0){
        Limsdetails[] list = LimsHandler.getInstance().libsdetails();
        String totbks=list[0].getTot_books();
        String totbrwdbk=list[0].getTot_borrowed_bks();
        int totbksint=Integer.parseInt(totbks);
        int totbrwdbksint=Integer.parseInt(totbrwdbk);
        float per=(num/totbrwdbksint)*100;          
%>
<font color="brown">
    <b>Total No of Books Available in Library is : <%=totbksint %><br></br>
    Out of which <%=totbrwdbksint %> are borrowed.<br></br>
    <b>No of readers reading Author 
        <%=request.getParameter("selectedauthor") %>'s book. : 
        <%=num %></b><br></br>
    <b> Author <%=request.getParameter("selectedauthor") %> is <%=per %> % 
        popular!</b><br></br>
</font>

<%}else{ %>
    <h4 align="center">
        <font color="red">
            <img border="0" src="images/close.PNG" ><br></br>
            Oops! some error occurred!
        </font>
    </h4>
<%
}
out.flush();
%>

<%} %>

这不是一个真正的JSP问题,而是Java如何处理整数算术。相关线路为:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;
执行整数/整数除法,然后乘以100。这将使用整数算术执行除法-因此结果将为0。将0乘以100仍然得到0

最简单的修复方法是将其中一个值设为浮点或双精度。例如:

int num = LimsHandler.getInstance().popularitycheck(...);    
float totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;
或者,您可以在表达式中强制转换:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / (float) totbrwdbksint) * 100;

此时,将使用浮点算法执行除法运算,您将得到预期的答案。

这不是一个真正的JSP问题,而是Java如何处理整数算法。相关线路为:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;
执行整数/整数除法,然后乘以100。这将使用整数算术执行除法-因此结果将为0。将0乘以100仍然得到0

最简单的修复方法是将其中一个值设为浮点或双精度。例如:

int num = LimsHandler.getInstance().popularitycheck(...);    
float totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / totbrwdbksint) * 100;
或者,您可以在表达式中强制转换:

int num = LimsHandler.getInstance().popularitycheck(...);
int totbrwdbksint = Integer.parseInt(totbrwdbk);
float per = (num / (float) totbrwdbksint) * 100;

此时,将使用浮点运算执行除法运算,您将得到预期的答案。

计算方式

float per=(num/totbrwdbksint)*100  
将num/totbrwdbksint的结果四舍五入为零

试一试


为了得到更好的结果。

你的计算方法

float per=(num/totbrwdbksint)*100  
将num/totbrwdbksint的结果四舍五入为零

试一试


以获得更好的结果。

这不是解决您最初问题的方法,但我建议您学习两个新东西:

JSTL CSS
您不应该以这种方式在JSP中嵌入Scriptlet或样式。总有一天,你会感谢自己付出的努力,因为维护和重新设置页面的样式会容易得多。

这并不能解决你原来的问题,但我建议你学习两个新东西:

JSTL CSS
您不应该以这种方式在JSP中嵌入Scriptlet或样式。总有一天,你会感谢自己付出的努力,因为维护和重新设置页面样式会容易得多。

我个人也更喜欢100*num/totbrwdbksint。如果num明显小于totbrwdbksint,那么可以提高一点精度。很好,它起作用了。thanx很多。你能再告诉我一件事吗。结果大约是26.323232%。我希望它能四舍五入到2位数。如何做到这一点?@extranon:另一方面,如果num非常大,它会导致潜在的溢出:我个人也更喜欢100*num/totbrwdbksint。如果num明显小于totbrwdbksint,那么可以提高一点精度。很好,它起作用了。thanx很多。你能再告诉我一件事吗。结果大约是26.323232%。我希望它能四舍五入到2位小数。如何做到这一点?@extranon:另一方面,如果num非常大,它会导致潜在的溢出:自2001年以来,Scriptlet就不受欢迎。在真实的Java类中编写Java代码。在JSP中使用taglibs/EL控制页面流和访问数据。自1998年以来,HTML标记已被弃用。使用CSS。请扔掉那些古书/教程,去买些像样的。这一切都像roseindia.net风格,请确保您不使用该网站作为参考。自2001年以来,不鼓励使用Scriptlet。在真实的Java类中编写Java代码。在JSP中使用taglibs/EL控制页面流和访问数据。自1998年以来,HTML标记已被弃用。使用CSS。请扔掉那些古书/教程,去买些像样的。这一切都像roseindia.net风格,请确保您不使用该网站作为参考。