final String[] caiming={"板栗山鸡","番茄鱼片","豆腐","甜汁三文鱼","清蒸大闸蟹","生鱼丝薄饼","土豆粉","猪蹄汤肉酿鲫鱼","北京烤鸭"};
String[] danjia={"10","20","25","10","15","20","28","40","25","15"};
for (int loopIndex=0; loopIndex < caiming.length; loopIndex++) {
TableItem item = new TableItem (table, SWT.NULL);

item.setText(0,caiming[loopIndex]);
item.setText(1,danjia[loopIndex]);==============================
请问如何对上面的单价进行计算?

解决方案 »

  1.   


    楼主的问题问得不太清楚:你要对单价进行怎样的计算?把相关的代码都贴出来吧。虽然我一直用Swing,对SWT不熟。不过其它东西是一样的,因此给个参考意见:double price = Double.parseDouble( danjia[loopIndex] ); // 把字符串转换成数值,就可以参与计算了
    double total = price * amount; // amount是商品数量
      

  2.   

    这个代码的关联不是很大。
    我是想对各个单价做加法,但是我不知道怎么提取出item中的数字。
      

  3.   

    那楼主再看看这段代码,这回应该对你有所帮助吧? double total = 0, price;
    for( int i = 0; i < table.getItemCount(); i++ )
    {
    TableItem item = table.getItem( i );
    price = Double.parseDouble( item.getText( 1 ) );
    total += price;
    }
    System.out.println( total );
      

  4.   

    这里是我为了实现楼主提出的功能而写的完整示例:
    http://topic.csdn.net/u/20081003/19/2ccdb1fe-c66f-4857-994b-86ef62b9e8cb.html感谢楼主!激发了本人对SWT的兴趣,并提供了练习题目,呵呵。