符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
你是想要看到每次的消费,然后在看到总价吗?那么你应该添加一个计数器,计算循环次数,把每次的zj添加到ArrayList中实现如下
创新互联主营潮南网站建设的网络公司,主营网站建设方案,重庆APP开发,潮南h5小程序开发搭建,潮南网站营销推广欢迎潮南等地区企业咨询
import java.util.*;
public class Shangjilianxi3 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name = "";
double danjia = 0.0;
int bianhao = 0;
int shuliang = 0;
double zhekou = 0.8;
double je = 0.0;
System.out.println("**************************************");
System.out.println("\n请选择购买的商品编号");
System.out.println("**************************************");
String wwe = "y";
//把每次的zj添加到list中
ListDouble list = new ArrayListDouble();
double zj = 0;
int index = 0;
while ("y".equals(wwe)) {
System.out.println("\n请输入商品编号");
bianhao = input.nextInt();
System.out.println("\n请输入购买数量");
shuliang = input.nextInt();
switch (bianhao) {
case 1:
name = "T桖";
danjia = 245.0;
break;
case 2:
name = "网球鞋";
danjia = 570.0;
break;
case 3:
name = "网球拍";
danjia = 320.0;
break;
}
zj = (danjia * shuliang);
System.out.println(name + "¥" + danjia + "\t数量" + shuliang + "合计¥"
+ zj);
//把每次zj添加到list中
list.add(zj);
System.out.println("是否继续(y/n)");
wwe = input.next();
//计算循环次数
index++;
}
//当大于一次时,把list中每个元素相加为zj
if (index = 1) {
//把上面的zj清零
zj = 0;
for (int i = 0; i list.size(); i++) {
zj += list.get(i);
}
}
System.out.println("折扣:" + zhekou);
System.out.println("应付金额" + zj);
zj = zj * zhekou;
System.out.println("实付金额" + zj);
je = input.nextInt();
System.out.println("找钱" + (je - zj));
}
}
用java Swing做的?给你个代码,还有运行结果图
package my.test.main;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.EmptyBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Purchase extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JLabel label_9;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Purchase frame = new Purchase();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Purchase() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("\u5546\u54C11");
label.setBounds(56, 40, 54, 15);
contentPane.add(label);
JLabel label_1 = new JLabel("\u5546\u54C12");
label_1.setBounds(56, 70, 54, 15);
contentPane.add(label_1);
JLabel label_2 = new JLabel("\u5546\u54C13");
label_2.setBounds(56, 100, 54, 15);
contentPane.add(label_2);
JLabel label_3 = new JLabel("\u5546\u54C14");
label_3.setBounds(56, 130, 54, 15);
contentPane.add(label_3);
JLabel label_4 = new JLabel("\u5546\u54C15");
label_4.setBounds(56, 160, 54, 15);
contentPane.add(label_4);
JLabel label_5 = new JLabel("\u5546\u54C1\u540D\u79F0");
label_5.setBounds(56, 7, 54, 15);
contentPane.add(label_5);
JLabel label_6 = new JLabel("单价");
label_6.setBounds(179, 7, 54, 15);
contentPane.add(label_6);
JLabel label_7 = new JLabel("数量");
label_7.setBounds(307, 7, 54, 15);
contentPane.add(label_7);
JLabel label_8 = new JLabel("总价");
label_8.setBounds(145, 224, 54, 15);
contentPane.add(label_8);
label_9 = new JLabel("0");
label_9.setBounds(209, 224, 54, 15);
contentPane.add(label_9);
JLabel label_10 = new JLabel("10");
label_10.setBounds(185, 40, 54, 15);
contentPane.add(label_10);
JLabel label_11 = new JLabel("20");
label_11.setBounds(185, 70, 54, 15);
contentPane.add(label_11);
JLabel label_12 = new JLabel("30");
label_12.setBounds(185, 100, 54, 15);
contentPane.add(label_12);
JLabel label_13 = new JLabel("40");
label_13.setBounds(185, 130, 54, 15);
contentPane.add(label_13);
JLabel label_14 = new JLabel("50");
label_14.setBounds(185, 160, 54, 15);
contentPane.add(label_14);
textField = new JTextField();
textField.setBounds(295, 37, 66, 21);
contentPane.add(textField);
textField.setColumns(10);
textField.addKeyListener(new CalcTotle());
textField_1 = new JTextField();
textField_1.setColumns(10);
textField_1.setBounds(295, 67, 66, 21);
contentPane.add(textField_1);
textField_1.addKeyListener(new CalcTotle());
textField_2 = new JTextField();
textField_2.setColumns(10);
textField_2.setBounds(295, 97, 66, 21);
contentPane.add(textField_2);
textField_2.addKeyListener(new CalcTotle());
textField_3 = new JTextField();
textField_3.setColumns(10);
textField_3.setBounds(295, 127, 66, 21);
contentPane.add(textField_3);
textField_3.addKeyListener(new CalcTotle());
textField_4 = new JTextField();
textField_4.setColumns(10);
textField_4.setBounds(295, 157, 66, 21);
contentPane.add(textField_4);
textField_4.addKeyListener(new CalcTotle());
}
class CalcTotle extends KeyAdapter {
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
int count1, count2, count3, count4, count5;
if (!textField.getText().isEmpty()) {
count1 = Integer.parseInt(textField.getText()) * 10;
} else {
count1 = 0;
}
if (!textField_1.getText().isEmpty()) {
count2 = Integer.parseInt(textField_1.getText()) * 20;
} else {
count2 = 0;
}
if (!textField_2.getText().isEmpty()) {
count3 = Integer.parseInt(textField_2.getText()) * 30;
} else {
count3 = 0;
}
if (!textField_3.getText().isEmpty()) {
count4 = Integer.parseInt(textField_3.getText()) * 40;
} else {
count4 = 0;
}
if (!textField_4.getText().isEmpty()) {
count5 = Integer.parseInt(textField_4.getText()) * 50;
} else {
count5 = 0;
}
final int totle = count1 + count2 + count3 + count4 + count5;
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
label_9.setText(totle + "");
}
});
}
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMapString, String hm=new HashMapString, String();
float totlePrice=0;
//添加book到购物车
public void addBook(String bookId,String bookQuantity){
if(hm.containsKey(bookId)){
int value=Integer.parseInt(hm.get(bookId));
value+=Integer.parseInt(bookQuantity);
hm.put(bookId, value+"");
}else{
hm.put(bookId, bookQuantity);
}
}
//修改数量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
//获取购物车的所有信息 并计算总价
public ArrayListBookBean getShoppingCart(){
ArrayListBookBean al=new ArrayListBookBean();
IteratorString i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空总价,防止无限累计
for(int j=0;jal.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//获取总价
public float getTotlePrice(){
return totlePrice;
}
//根据ID获取数量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空购物车
public void clear(){
hm.clear();
}
//删除购物车中的一本书
public void deleteById(String id){
hm.remove(id);
}
}
for(int i=0;i商品一的数量;i++){
// 判断当前价格(商品一单价 乘以 数量i)
// 等于总价保存当前组合
// 大于 continue 退出当前循环
// 小于 继续下层循环
for(int j=0;j商品二的数量;j++){
// 判断当前价格(商品一单价 乘以 数量i + 商品二单价 乘以 数量j )
// 等于总价保存当前组合
// 大于 continue 退出当前循环
// 小于 继续下层循环
for(int k=0;k商品三的数量;k++){
// 判断当前价格(商品一单价 乘以 数量i + 商品二单价 乘以 数量j + 商品三单价 乘以 数量k)
// 等于总价保存当前组合
// 大于 continue 退出当前循环
// 小于 继续下层循环
.......
}
}
}
这么算时间复杂度肯定不是最优的
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("计算"); 界面会更合理一点
在单价和数量的文本框上绑定一个失去焦点事件,addFocusListener,然后重写失去焦点方法,里面判断一下单价和数量文本框是否都有值,如果都有值,就进行计算,然后写到总价后面的文本框。