
正文
计算总价钱java代码 使用js代码算出总价
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java难题,请求java大神帮助,谢谢各位?
题主可参考我的答案,亲测可用, 如果有什么不懂可追问
首先建立商品类:
public class Product {
/**
* 商品编号
*/
private String number;
/**
* 商品名称
*/
private String name;
/**
* 商品价格
*/
private float price;
/**
* 商品数量
*/
private int quantity;
public float getPrice() {
return price;
}
public int getQuantity() {
return quantity;
}
public Product(String number, String name, float price, int quantity) {
this.number = number;
this.name = name;
this.price = price;
this.quantity = quantity;
}
public void printProduct() {
System.out.println("商品编号:" + this.number);
System.out.println("商品名称:" + this.name);
System.out.println("商品价格:" + this.price);
System.out.println("商品数量:" + this.quantity);
}
}
2. 再建立测试类,直接运行main方法即可
public class ProductTest {
public static void main(String[] args) {
ListProduct productList = new ArrayList();
Product product1 = new Product("001", "商品1", 1f, 3);
Product product2 = new Product("002", "商品2", 1f, 1);
Product product3 = new Product("003", "商品3", 1f, 5);
Product product4 = new Product("004", "商品4", 1f, 2);
productList.add(product1);
productList.add(product2);
productList.add(product3);
productList.add(product4);
double total = 0;
for (Product product : productList) {
product.printProduct();
total += product.getPrice() * product.getQuantity();
}
System.out.println("商品总价格:" + total);
}
}
答案如下
上面的两个类可以放在同一包下即可
相关问答
Q1: 关于JAVA的问题,已知苹果单价,输入重量 求总价,乘法怎么插入。 求高手指点,我是初学
public void actionPerformed(ActionEvent e)
{
output.setText(input.getText());
}
将上述代码改成:
public void actionPerformed(ActionEvent e)
{
String sw=input.getText(); //获取输入重量
if(!sw.trim().equals("")){ //判断重量文本框中的内容不为空时才执行if语句
double w=Double.parseDouble(sw); //将重量字符串转换成double型
double price=w*6; //将重量*单价 得到总价
output.setText(price+"元"); //在输出文本框中输入
}
}
另外,再将代码中的 btn=new Button("清零");改成 btn=new Button("计算"); 界面会更合理一点
Q2: 一组价格不同、数量不同且有限的商品,要求组合商品数量达到指定的总价 求编程思路(java)或代码
for(int i=0;i商品一计算总价钱java代码的数量;i++){
// 判断当前价格(商品一单价 乘以 数量i)
// 等于总价保存当前组合
// 大于 continue 退出当前循环
// 小于 继续下层循环
for(int j=0;j商品二计算总价钱java代码的数量;j++){
// 判断当前价格(商品一单价 乘以 数量i + 商品二单价 乘以 数量j )
// 等于总价保存当前组合
// 大于 continue 退出当前循环
// 小于 继续下层循环
for(int k=0;k商品三的数量;k++){
// 判断当前价格(商品一单价 乘以 数量i + 商品二单价 乘以 数量j + 商品三单价 乘以 数量k)
// 等于总价保存当前组合
// 大于 continue 退出当前循环
// 小于 继续下层循环
.......
}
}
}
这么算时间复杂度肯定不是最优的
Q3: 问一个关于java的程序代码
import java.util.Scanner;
public class colPrice{
public static void main(String[] args){
double[] price = new double[10];
double totalPrice = 0;
System.out.println("请输入商品价格:");
for(int i=0;i10;i++){
System.out.print(i+1+": ");
Scanner in = new Scanner( System.in);
price[i] = in.nextDouble();
}
System.out.println("");
for(int i= 0;iprice.length;i++){
totalPrice+=price[i];
}
System.out.println("商品总价格是:"+totalPrice);
}
}
Q4: java 循环计算金额
如果你的money1 是所有商品的总价, money 是每种商品的价格
上面所写的code 就是所有商品的价格。
int money1=0;
for(int i =0; i list.size(); i++)
{
Commodity commoditys = (Commodity ) list.get(i); #得到商品
double money= (commoditys.getNumber()*commoditys.getPrice());#该商品的单价乘以数量
money1=money1+money; #得到总价
}
System.out.println("Total price will be: ", money1); #输出总价
关于计算总价钱java代码和使用js代码算出总价的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







