
正文
算单价的java代码 java计算打折扣
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
关于JAVA的问题,已知苹果单价,输入重量 求总价,乘法怎么插入。 求高手指点,我是初学
public void actionPerformed(ActionEvent e)
{
output.setText(input.getText());
}
将上述代码改成算单价的java代码:
public void actionPerformed(ActionEvent e)
{
String sw=input.getText(); //获取输入重量
if(!sw.trim().equals("")){ //判断重量文本框中算单价的java代码的内容不为空时才执行if语句
double w=Double.parseDouble(sw); //将重量字符串转换成double型
double price=w*6; //将重量*单价 得到总价
output.setText(price+"元"); //在输出文本框中输入
}
}
另外算单价的java代码,再将代码中的 btn=new Button("清零");改成 btn=new Button("计算"); 界面会更合理一点
相关问答
Q1: java超市计价系统代码
package entity;
public class Market {
private int id;//id
private int num;//数量
private String goods;//商品
private double price;//价格
public Market(int id, int num, String goods, double price) {
super();
this.id = id;
this.num = num;
this.goods = goods;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getGoods() {
return goods;
}
public void setGoods(String goods) {
this.goods = goods;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double calc( ){
double sum=price*num;
System.out.println("您消费共计:"+sum+"¥");
return sum;
}
}
package test;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import entity.Market;
public class Test {
private static MapInteger,Market goods=new HashMapInteger, Market();
public static void main(String[] args) {
System.out.println("-------超市计价系统-------");
String goods1="可口可乐";
String goods2="爆米花";
String goods3="益达";
printTable("编号","商品","价格");
printTable("1",goods1,"3.0¥");
printTable("2",goods2,"5.0¥");
printTable("3",goods3,"10.0¥");
goods.put(1, new Market(1, 1, goods1, 3.0));
goods.put(2, new Market(2, 1, goods2, 5.0));
goods.put(3, new Market(3, 1, goods3, 10.0));
Scanner input = new Scanner(System.in);
System.out.println("请输入商品的编号:");
int num = input.nextInt();
System.out.println("请输入商品的数量");
int amount = input.nextInt();
Market market = goods.get(num);
market.setNum(amount);
market.calc();
}
private static void printTable(String row1,String row2,String row3 ) {
System.out.print(row1);
int times=12;
if (row2!="商品") {
times=5;
}
for (int i = 0; i times; i++) {
System.out.print(" ");
}
System.out.print(row2);
for (int i = 0; i 10; i++) {
System.out.print(" ");
}
System.out.print(row3);
System.out.println("\n");
}
}
//测试结果:
-------超市计价系统-------
编号 商品 价格
1 可口可乐 3.0¥
2 爆米花 5.0¥
3 益达 10.0¥
请输入商品的编号:
3
请输入商品的数量
5
您消费共计:50.0¥
Q2: 用Java编写输入某一商品名称,查询其单价和数量
import java.util.Scanner;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int name;
Scanner sc = new Scanner(System.in);
System.out.println("请输入你想要查询的商品名称");
name=sc.nextInt();
switch(name){
case 1:System.out.println(" 苹果: 价格:5元; 数量:十个");break;
case 2:System.out.println(" 香蕉: 价格:3元; 数量:十一个");break;
case 3:System.out.println(" 梨子: 价格:5.9元; 数量:八个");break;
default :System.out.println(" 抱歉找不到该商品");break;
}
}
}
Q3: java难题,请求帮助,谢谢各位大神!请给我代码?
定义一个Product类,如下:
public class Product {
private String name;//商 品名
private String price;//单价
private String num;//数量
private double total;//商 品总价值
public Product(String name , String price, String num){this . name=name ;
this. price=price;
this . num=num;
public void showTotal(){
price=double(price);
num=Java.lang.Integer.parseInt(num);
total=price*num;
//在此完成该商品总价值的计算
System. out . print1n("商品"+name+"的总价值是: "+total)}
在showTotal()方法中补齐代码,利用包装类的方法把String类型转换成数值,计算商品的总价值;
public class TestProduct{
public static void main(String a[]){
new Product("cup","12.00","20").showTotal();
System.out.println("This is a test for class-Product");
}
}
//定义一个测试类,在main方法中创建Product对象,并调用它的showTotal()方法。
Q4: java问题 显示水果的价格
1、price=0;
switch(choice){
case 1:price=3f;break;
case 2:price=2.5f;break;
case 3:price=4.1f;break;
case 4:price=10.2f;break;
}
2、price=1;
for(int i=1;i=choice;i++){
price*=i;
}
关于算单价的java代码和java计算打折扣的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







