java總價(jià)代碼 java計(jì)算總價(jià)格

java語(yǔ)句中循環(huán)問(wèn)題,求多次循環(huán)的總價(jià)。

你是想要看到每次的消費(fèi),然后在看到總價(jià)嗎?那么你應(yīng)該添加一個(gè)計(jì)數(shù)器,計(jì)算循環(huán)次數(shù),把每次的zj添加到ArrayList中實(shí)現(xiàn)如下

創(chuàng)新互聯(lián)主營(yíng)潮南網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶APP開發(fā),潮南h5小程序開發(fā)搭建,潮南網(wǎng)站營(yíng)銷推廣歡迎潮南等地區(qū)企業(yè)咨詢

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請(qǐng)選擇購(gòu)買的商品編號(hào)");

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請(qǐng)輸入商品編號(hào)");

bianhao?=?input.nextInt();

System.out.println("\n請(qǐng)輸入購(gòu)買數(shù)量");

shuliang?=?input.nextInt();

switch?(bianhao)?{

case?1:

name?=?"T桖";

danjia?=?245.0;

break;

case?2:

name?=?"網(wǎng)球鞋";

danjia?=?570.0;

break;

case?3:

name?=?"網(wǎng)球拍";

danjia?=?320.0;

break;

}

zj?=?(danjia?*?shuliang);

System.out.println(name?+?"¥"?+?danjia?+?"\t數(shù)量"?+?shuliang?+?"合計(jì)¥"

+?zj);

//把每次zj添加到list中

list.add(zj);

System.out.println("是否繼續(xù)(y/n)");

wwe?=?input.next();

//計(jì)算循環(huán)次數(shù)

index++;

}

//當(dāng)大于一次時(shí),把list中每個(gè)元素相加為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("應(yīng)付金額"?+?zj);

zj?=?zj?*?zhekou;

System.out.println("實(shí)付金額"?+?zj);

je?=?input.nextInt();

System.out.println("找錢"?+?(je?-?zj));

}

}

使用JAVA 編寫一個(gè)程序,顯示5中商品價(jià)格,用戶可以選擇多種商品并在其后的文本框輸入購(gòu)買的數(shù)量。

用java Swing做的?給你個(gè)代碼,還有運(yùn)行結(jié)果圖

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("單價(jià)");

label_6.setBounds(179,?7,?54,?15);

contentPane.add(label_6);

JLabel?label_7?=?new?JLabel("數(shù)量");

label_7.setBounds(307,?7,?54,?15);

contentPane.add(label_7);

JLabel?label_8?=?new?JLabel("總價(jià)");

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?+?"");

}

});

}

}

}

購(gòu)物車的Java代碼

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到購(gòu)物車

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);

}

}

//修改數(shù)量

public void updateQuantity(String bookId,String bookQuantity){

hm.put(bookId, bookQuantity);

}

//獲取購(gòu)物車的所有信息 并計(jì)算總價(jià)

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; //清空總價(jià),防止無(wú)限累計(jì)

for(int j=0;jal.size();j++){

BookBean bb=al.get(j);

totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));

}

return al;

}

//獲取總價(jià)

public float getTotlePrice(){

return totlePrice;

}

//根據(jù)ID獲取數(shù)量

public String getQuantityById(String id){

String quantity=hm.get(id);

return quantity;

}

//清空購(gòu)物車

public void clear(){

hm.clear();

}

//刪除購(gòu)物車中的一本書

public void deleteById(String id){

hm.remove(id);

}

}

一組價(jià)格不同、數(shù)量不同且有限的商品,要求組合商品數(shù)量達(dá)到指定的總價(jià) 求編程思路(java)或代碼

for(int?i=0;i商品一的數(shù)量;i++){

//?判斷當(dāng)前價(jià)格(商品一單價(jià)?乘以?數(shù)量i)

//?等于總價(jià)保存當(dāng)前組合

//?大于?continue?退出當(dāng)前循環(huán)

//?小于?繼續(xù)下層循環(huán)???

for(int?j=0;j商品二的數(shù)量;j++){

//?判斷當(dāng)前價(jià)格(商品一單價(jià)?乘以?數(shù)量i??+???商品二單價(jià)?乘以?數(shù)量j?)

//?等于總價(jià)保存當(dāng)前組合

//?大于?continue?退出當(dāng)前循環(huán)

//?小于?繼續(xù)下層循環(huán)

for(int?k=0;k商品三的數(shù)量;k++){

//?判斷當(dāng)前價(jià)格(商品一單價(jià)?乘以?數(shù)量i??+???商品二單價(jià)?乘以?數(shù)量j?+?商品三單價(jià)?乘以?數(shù)量k)

//?等于總價(jià)保存當(dāng)前組合

//?大于?continue?退出當(dāng)前循環(huán)

//?小于?繼續(xù)下層循環(huán)

.......

}

}

}

這么算時(shí)間復(fù)雜度肯定不是最優(yōu)的

關(guān)于JAVA的問(wèn)題,已知蘋果單價(jià),輸入重量 求總價(jià),乘法怎么插入。 求高手指點(diǎn),我是初學(xué)

public void actionPerformed(ActionEvent e)

{

output.setText(input.getText());

}

將上述代碼改成:

public void actionPerformed(ActionEvent e)

{

String sw=input.getText(); //獲取輸入重量

if(!sw.trim().equals("")){ //判斷重量文本框中的內(nèi)容不為空時(shí)才執(zhí)行if語(yǔ)句

double w=Double.parseDouble(sw); //將重量字符串轉(zhuǎn)換成double型

double price=w*6; //將重量*單價(jià) 得到總價(jià)

output.setText(price+"元"); //在輸出文本框中輸入

}

}

另外,再將代碼中的 btn=new Button("清零");改成 btn=new Button("計(jì)算"); 界面會(huì)更合理一點(diǎn)

Java輸入單價(jià)和數(shù)量,馬上輸出總價(jià)

在單價(jià)和數(shù)量的文本框上綁定一個(gè)失去焦點(diǎn)事件,addFocusListener,然后重寫失去焦點(diǎn)方法,里面判斷一下單價(jià)和數(shù)量文本框是否都有值,如果都有值,就進(jìn)行計(jì)算,然后寫到總價(jià)后面的文本框。

新聞標(biāo)題:java總價(jià)代碼 java計(jì)算總價(jià)格
網(wǎng)頁(yè)路徑:http://muchs.cn/article46/hgighg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)、全網(wǎng)營(yíng)銷推廣、定制開發(fā)微信小程序、品牌網(wǎng)站制作Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管