代码之家  ›  专栏  ›  技术社区  ›  WiLLyxVKei

列表换位

  •  2
  • WiLLyxVKei  · 技术社区  · 8 年前

    我想做的是将我的列表进行换位。确切地说,是从列表到表格。

    problem description

    在while im接收保存在数组{13}中的值时(此数组初始化为0。每月一个,不使用前0)

    month           =  rsSol.getInt("month");      // To save number of month
    mon_amounth     =  rsSol.getDouble("amounth"); // Save amount in a value
    values[month]   =  mon_amounth;                // Save amount in the array
    monthTotal     +=  giro_montoMes;              // Sum of each mon_amounth
    

    那么,这就是我的代码中我被绊倒的部分。

    我对需要添加到组描述并在同一行中打印所有金额的条件有点困惑。

    while ( rsSol.next() ) {
        //where i (re)write description
        if ( desc.equals(empty_string) ){
            desc        =  rsSol.getString("description");
        }
    
        if ( !desc.equals(rsSol.getString("giro")) && count > 1 ){
            desc        = rsSol.getString("giro");
        } 
    
        // Where i print the description
        <tr class>
            <td border='0' ALIGN='left'>    desc.toLowerCase()  </td>
    
            // Where i print the array
            for(int i=1; i<=12; i++){
                <td border='0' ALIGN='right'>   values[i])  </td>
            }
    
            // Where i print the sum of all months
            <td border='0' ALIGN='right'>   monthTotal  </td>
        </tr>
    
        // Setting values to 0 and cleaning the array
        mon_amounth =   0;
    
        for ( int i=1 ; i<=12 ; i++ ) {
            values[i] = 0;
        }
    }
    

    任何帮助都会非常有用。 提前谢谢。

    1 回复  |  直到 8 年前
        1
  •  1
  •   Dmitrii Cheremisin    8 年前

    您需要迭代结果集,然后打印总金额。 我想你有不同的描述,应该计算总数。 您可以使用HashMap,该HashMap不允许重复键作为描述,不允许重复值作为总量。

    // collect sums
        Map<String, Integer[]> totals = new HashMap<>();
        while ( rsSol.next() ) {
            if ( desc.equals("") ){
                desc =  rsSol.getString("description");
            }
            if ( !desc.equals(rsSol.getString("giro")) && count > 1 ){
                desc = rsSol.getString("giro");
            }
            totals.putIfAbsent(desc, new Integer[12]);
            Integer[] totalSums = totals.get(desc);
            for(int i = 0; i < 12; i++){
                totalSums += values[i];
            }
        }
    
    // print your results
        for (String desc : totals.keySet()) {
           <tr class>
                <td border='0' ALIGN='left'>    desc.toLowerCase()  </td>
    
                    // Where i print the array
                    for(int i=1; i<=12; i++){
                    <td border='0' ALIGN='right'>   totals.get(desc)  </td>
                    }
    
                    // Where i print the sum of all months
                <td border='0' ALIGN='right'>   monthTotal  </td>
            </tr> 
        }
    
    推荐文章