輸出商品內(nèi)容Java代碼 java輸入商品單價和數(shù)量求總價

. 設(shè)計一個Java程序,其功能是根據(jù)商品的價格輸出不同的信息:(使用switch – case結(jié)

輸出一個價格數(shù)據(jù)

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的零陵網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

然后通過switch語句在case中的一個區(qū)域內(nèi)比較確定商品的價格再輸出

如果不想自己寫代碼的話支付寶5元可以幫你搞定

java輸入輸出程序代碼

概述

java程序輸入需要用到Scanner工具類,輸出則使用println

解析

1、輸入:

從鍵盤輸入需要用到j(luò)ava的Scanner這個util包下的工具類

Scanner中輸入是需要對輸入的字符進行分類,int類型為nextint()方法,double類型為nextDouble()方法,字符串類型為next()方法。

#code:

#out:

2、輸出:

java常用的輸出為println

#code:

#out:

除了println()方法之外,java還有print()和printf()兩種輸出方法,

print、println、printf的區(qū)別:

print--是函數(shù),可以返回一個值,只能有一個參數(shù)。

println 將它的參數(shù)顯示在命令窗口,并在結(jié)尾加上換行符,將輸出光標定位在下一行的開始。

printf--函數(shù),把文字格式化以后輸出,直接調(diào)用系統(tǒng)調(diào)用進行IO的,他是非緩沖的。

拓展內(nèi)容

java 輸入輸出流(I/O)

Java中I/O操作主要是指使用Java進行輸入,輸出操作.?Java所有的I/O機制都是基于數(shù)據(jù)流進行輸入輸出,這些數(shù)據(jù)流表示了字符或者字節(jié)數(shù)據(jù)的流動序列。Java的I/O流提供了讀寫數(shù)據(jù)的標準方法。任何Java中表示數(shù)據(jù)源的對象都會提供以數(shù)據(jù)流的方式讀寫它的數(shù)據(jù)的方法。

Java.io是大多數(shù)面向數(shù)據(jù)流的輸入/輸出類的主要軟件包。此外,Java也對塊傳輸提供支持,在核心庫 java.nio中采用的便是塊IO。

流IO的好處是簡單易用,缺點是效率較低。塊IO效率很高,但編程比較復(fù)雜。

Java IO模型 ?:

Java的IO模型設(shè)計非常優(yōu)秀,它使用Decorator模式,按功能劃分Stream,您可以動態(tài)裝配這些Stream,以便獲得您需要的功能。例如,您需要一個具有緩沖的文件輸入流,則應(yīng)當組合使用FileInputStream和BufferedInputStream。

輸入流(Input ?Stream):

程序從輸入流讀取數(shù)據(jù)源。數(shù)據(jù)源包括外界(鍵盤、文件、網(wǎng)絡(luò)…),即是將數(shù)據(jù)源讀入到程序的通信通道。

輸出流(output Stream):

程序向輸出流寫入數(shù)據(jù)。將程序中的數(shù)據(jù)輸出到外界(顯示器、打印機、文件、網(wǎng)絡(luò)…)的通信通道。

java 商品 系統(tǒng) 代碼

package entity;

public class Market {

private int id;//id

private int num;//數(shù)量

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("-------超市計價系統(tǒng)-------");

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("請輸入商品的數(shù)量");

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

}

}

//測試結(jié)果:

-------超市計價系統(tǒng)-------

編號 商品 價格

1 可口可樂 3.0¥

2 爆米花 5.0¥

3 益達 10.0¥

請輸入商品的編號:

3

請輸入商品的數(shù)量

5

您消費共計:50.0¥

高懸賞求java解答,求編寫一個題,就是做一個菜單,要求有3個商品,要求輸入名字和價格,然后輸出出

import?java.io.File;

import?java.io.FileWriter;

import?java.io.IOException;

import?java.io.OutputStream;

import?java.io.Writer;

import?java.util.Scanner;

public?class?menu?{

public?static?void?main(String[]?args)?{

//?TODO?Auto-generated?method?stub

StringBuffer?buffer?=new?StringBuffer();

String[]?num={"first","second","third"};

//變量i?為輸入的次數(shù)。?如果有變動?直接修改即可。

for(int?i?=?0?;i3;i++){

System.out.println("Please?enter?the?name?of?the?"+num[i]+"?item:");

Scanner?nameSc?=?new?Scanner(System.in);?

buffer.append("("+nameSc.next()+"/");

System.out.println("Please?enter?the?"+num[i]+"?item?price:");

nameSc?=?new?Scanner(System.in);?

buffer.append(nameSc.next()+")");

if(i2)

buffer.append("\r\n");

}

System.out.println(buffer.toString());

try?{

//1、打開流

Writer?w=new?FileWriter("C://menu.txt",true);

//2、寫入內(nèi)容

w.write(buffer.toString());

//3、關(guān)閉流

w.close();

}?catch?(IOException?e)?{

System.out.println("文件寫入錯誤:"+e.getMessage());

}

}

}

//代碼寫的比較low ? 看不慣勿噴。 如果與你要求有出入,直接追問

求java解答,求編寫一個題,就是做一個菜單,要求有3個商品,要求輸入名字和價格,然后輸出出來,輸

import?java.io.File;

import?java.io.FileOutputStream;

import?java.io.OutputStream;

import?java.util.Scanner;

public?class?Demo02?{

public?static?void?main(String[]?args)?throws?Exception?{

StringBuffer?sb?=?new?StringBuffer();

Scanner?sc?=?new?Scanner(System.in);

for?(int?i?=?0;?i??3;?i++)?{

System.out.print("請輸入商品名字:\r\n");

String?name?=?sc.nextLine();

System.out.print("請輸入商品價格:\r\n");

String?price?=?sc.nextLine();

sb.append("("+name+"/"+price+")\r\n");

}

File?file?=?new?File("C:\\MyData.txt");

if(!file.exists()){

file.createNewFile();

}

OutputStream?os?=?new?FileOutputStream(file);

byte[]?bytes?=?sb.toString().getBytes();

os.write(bytes,?0,?bytes.length);

os.close();

sc.close();

}

}

Java 創(chuàng)建一個長度為5的String數(shù)組,存儲商品名稱。使用循環(huán)輸出商品名

public?class?demo2

{

public?static?void?main(String[]agrs)

{

String[]?str=new?String[5];

str[0]="牙膏";

str[1]="牙刷1";

str[2]="牙刷2";

str[3]="牙刷3";

str[4]="牙刷4";

for(int?i=0;istr.length;i++)

{

System.out.print(str[i]);

}

}

}

網(wǎng)站欄目:輸出商品內(nèi)容Java代碼 java輸入商品單價和數(shù)量求總價
當前URL:http://muchs.cn/article2/docdpoc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作品牌網(wǎng)站建設(shè)、用戶體驗網(wǎng)站收錄、網(wǎng)站排名

廣告

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

成都做網(wǎng)站