for (commodity2 product : commodities) { commodity2 stu = commodities.get(); totalPrice = totalPrice + stu.getPrice() * stu.getNo(); }这段代码应如何改对
时间: 2024-02-14 17:04:30 浏览: 20
首先,`commodities.get()`方法是不能在循环内使用的,因为它会返回当前列表中的第一个元素,而不是遍历到的元素。正确的做法是使用循环变量 `product`,也就是 `for` 循环的迭代变量。
其次,计算总价的方式也有问题,应该是将每个商品的单价乘以数量,然后累加起来。
改正后的代码如下:
```
for (commodity2 product : commodities) {
totalPrice = totalPrice + product.getPrice() * product.getNo();
}
```
这样就可以正确地计算出购买所有商品的总价了。
相关问题
totalPriceView = findViewById(R.id.total_price); for (commodity2 product : commodities) { totalPrice += commodity2.getprice() * commodity.getNo(); } totalPriceView.setText("$" + totalPrice); } 这段代码中的getprice()我在commodity中定义了方法,但是他的报错要求我用静态的方法是为什么
在这段代码中,您正在使用静态方法`commodity2.getprice()`来获取商品的价格,而不是使用实例方法。这可能是因为您没有实例化`commodity2`类的对象,或者您正在从静态上下文中调用该方法(例如,从静态方法或类中调用它)。
如果您想使用实例方法,您需要实例化`commodity2`类的对象,例如:
```
commodity2 product = new commodity2();
totalPrice += product.getprice() * commodity.getNo();
```
如果您想继续使用静态方法,请确保该方法也被声明为静态方法,并且您从静态上下文中调用它。
SELECT COUNT(*) FROM (SELECT t.* from ( SELECT p.*,p.id descId from package_channel_price_plan p as a where a.sys_default=0 UNION SELECT c.*,c.id descId from package_commodity_main_info as b LEFT JOIN package_channel_price_plan as c on b.plan_code=c.plan_code where 1=1 and b.commodity_code = ? ) as t where 1=1 and t.plan_status = ? order by descId=? desc, t.updated_time DESC) TOTAL 这个ssql 错在哪
这个 SQL 语句中的错误在于使用了 "as" 关键字来给子查询中的表起别名,但是在子查询中不能使用 "as" 关键字来为表起别名。正确的写法应该是直接使用表名来引用子查询的结果。以下是修正后的 SQL 语句:
SELECT COUNT(*) FROM (SELECT t.* from (
SELECT p.*,p.id descId from package_channel_price_plan p
where p.sys_default=0
UNION
SELECT c.*,c.id descId from package_commodity_main_info b
LEFT JOIN package_channel_price_plan c on b.plan_code=c.plan_code
where 1=1 and b.commodity_code = ?
) t
where 1=1 and t.plan_status = ?
order by descId=? desc, t.updated_time DESC) TOTAL