值不会在for循环外打印

时间:2013-12-18 09:03:47

标签: servlets

在我的程序中,我的实验室练习的一个简单的购物应用程序,我只是计算了for循环中的项目的价格,但是当我尝试在外面打印它时没有打印...请给我一些建议。

for (int i = 1; i < 7; i++) {
            String selection = request.getParameter("a" + i);

            if (selection.equals("l")) {

                price = Integer.parseInt(request.getParameter("b" + i));
                total = total + price;

                out.println("<h3>You have purchased the item:<br>Price is:</h3>" + price);
            }
        }
        out.println("THE TOTAL IS"+total);
        out.println("</body>");
        out.println("</html>");

2 个答案:

答案 0 :(得分:0)

 String selection = request.getParameter("a" + i);
 //Here print the selection there is the problem
 if (selection.equals("l")) ///Here alway it is false thats why you are getting this.
 {
 }

答案 1 :(得分:-1)

由于超出范围,因此无法打印。你的total  变量只存在于循环中。

你有基本的编程技巧并且知道变量范围,对吗?

http://www.java-made-easy.com/variable-scope.html