重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.util.Scanner;
专注于为中小企业提供网站建设、成都网站设计服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业永泰免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
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);
}
}
用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 + "");
}
});
}
}
}
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 退出当前循环
// 小于 继续下层循环
.......
}
}
}
这么算时间复杂度肯定不是最优的