【问题标题】:Double amt of Current Balance getting displayed in my simple web project当前余额的双倍显示在我的简单 Web 项目中
【发布时间】:2019-05-26 06:25:16
【问题描述】:

代码:

public static int getBalance()
{         
    List <Integer> sum = new ArrayList<Integer>();

    int balance=0;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/money","root","cpp");
        PreparedStatement ps = con.prepareStatement("select * from account");
        ResultSet rs = ps.executeQuery();
        while(rs.next())
        {
            sum.add(Integer.parseInt(rs.getString(1)));
            for(int i:sum)
            {
                balance += i;   
            }

        }
    }
}

当显示金额时,它已被加倍 例如,如果 2000 只是当前金额,则当前余额为 4000

【问题讨论】:

    标签: java mysql html jsp


    【解决方案1】:

    您为找到的每个结果执行一次,因此如果您有两个结果,则金额将相加两次:

                for(int i:sum)
                 {
                     balance+=i;
    
                 }
    

    尝试将该块移出while 语句。您可以在此处了解有关while 的更多信息:https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html

    【讨论】:

    • 再次完成 tysm
    猜你喜欢
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    相关资源
    最近更新 更多