代码麻烦粘贴一下吧
public class Demo {
/**
* 商品信息类 (用于存储商品单价与商品数量)
*/
static class ProductInfo{
/**单价*/
private double price;
/**数量*/
private int number;
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//定义产品集合存储输入的产品信息
List<ProductInfo> productInfoList = new ArrayList<>();
//监听输入将符合要求的产品信息存入产品集合对象(productInfoList)中
while ( sc.hasNextLine()) {
String str = sc.nextLine();
if( str != null && str.trim().length() > 0) {
//字符串比较不要使用=号,当输入 "0 0"时,跳出循环
if( str.trim().equals("0 0")) break;
String [] strArr = str.split(" ");
if( strArr.length > 1) {
//创建产品对象,并保存输入的产品信息
ProductInfo productInfo = new ProductInfo();
productInfo.setPrice(Double.parseDouble(strArr[0]));
productInfo.setNumber(Integer.parseInt(strArr[1]));
//将产品对象放入集合中
productInfoList.add(productInfo);
}
}
}
//遍历并计算输入的产品信息
System.out.println("计算并输出价格");
productInfoList.forEach((productInfo -> {
double totalPrice = productInfo.getPrice() * productInfo.getNumber();
System.out.println(String.format("%.2f", totalPrice));
}));
}
}
如有帮助,请采纳!!!!