提示:文章寫完后,目錄可以自動生成,如何生成可參考右邊的幫助文檔
前言
記錄第一個Java實驗
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、任務(wù)介紹
1.任務(wù)描述
編寫一個超市購物程序,在一家超市有牙刷、毛巾、水杯、蘋果和香蕉五種商品,商品價格如下表所示。
編號 商品名稱 價格
1 牙刷 8.8元
2 毛巾 10.0元
3 水杯 18.8元
4 蘋果 12.5元
5 香蕉 15.5元
用戶輸入商品序列號進行商品購買,用戶輸入購買數(shù)量后計算出所需要花費的錢,一次購買結(jié)束后,需要用戶輸入“Y”或“N”,“Y”代表繼續(xù)購買,“N”代表購物結(jié)束。
2.運行結(jié)果
任務(wù)運行結(jié)果如圖2-1所示。
圖2-1 運行結(jié)果
3.任務(wù)目標(biāo)
? 學(xué)會分析” 超市購物 ”程序的實現(xiàn)思路。
? 根據(jù)思路獨立完成” 超市購物 ”的源代碼編寫、編譯及運行。
? 掌握在程序中使用while循環(huán)結(jié)構(gòu)和switch循環(huán)結(jié)構(gòu)語句進行運算操作。
4.實現(xiàn)思路
(1) 從運行結(jié)果可以看出,我們需要先定義5個商品的價格,double類型,再打印出5種商品的價格。
(2) 從運行結(jié)果可以看出,這里我們讓用戶通過鍵盤輸入購買商品的序列號以及購買的數(shù)量,需要使用到Scanner類,步驟如下:
1)導(dǎo)包: import java.util.Sanner;
2)創(chuàng)建輸入流對象:Scanner sc = new Scanner(System.in);
3) 通過對象調(diào)用相應(yīng)的next方法獲取相應(yīng)類型數(shù)值:
如int a=sc.nextInt( ); 獲取整型數(shù)值的輸入
String str=sc.nextLine( ); 獲取字符串的輸入
(3) 從運行結(jié)果可以看出,需要循環(huán)選擇購買的switch多分支語句,這里使用while嵌套switch語句即可以達(dá)到目的,使得用戶可以反復(fù)選擇購買商品,和輸入購買商品的數(shù)量,直到用戶購買完當(dāng)前商品后選擇不繼續(xù)購買。
二、程序?qū)崿F(xiàn)
這里我自由發(fā)揮多加了點東西…
1.代碼
import java.util.Scanner;
public class Product {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double toothbrush=8.8; //牙刷價格
double towel=10.0; //毛巾價格
double cup=18.8; //水杯價格
double apple=12.5; //蘋果價格
double banana=15.5; //香蕉價格
double qqsugar=1.5; //QQ糖價格
String choose = "Y";
System.out.println("--------你好!歡迎光臨不買白不買小店--------");
System.out.println("--------本店新進了五樣商品,您可以在其中挑選心儀的商品購買--------");
System.out.println("1.牙刷價格" + toothbrush + "元");
System.out.println("2.毛巾價格" + towel + "元");
System.out.println("3.水杯價格" + cup + "元");
System.out.println("4.蘋果價格" + apple + "元");
System.out.println("5.香蕉價格" + banana + "元");
System.out.println("6.QQ糖價格" + qqsugar + "元");
int total = 0;
double Total_price = 0;
while (choose.equals("Y")||choose.equals("y")){
System.out.println("請輸入您需要購買商品的序列號:");
int id =sc.nextInt( );
switch (id){
case 1:
System.out.println("牙刷價格為:" +toothbrush+ "元,請輸入您需要購買的數(shù)量:");
int s = sc.nextInt();
double d = s * toothbrush;
System.out.println("您購買的牙刷數(shù)量為:" + s + "支,共消費:" + d + "元");
total += s;
Total_price += d;
System.out.println("是否繼續(xù)購買(Y,繼續(xù) N,退出):");
choose = sc.next();
break;
case 2:
System.out.println("毛巾價格為:" + towel + "元,請輸入您需要購買的數(shù)量:");
int s1 = sc.nextInt();
double d1 = s1 * towel;
System.out.println("您購買毛巾數(shù)量為:" + s1 + "條,共消費:" + d1 + "元");
total += s1;
Total_price += d1;
System.out.println("是否繼續(xù)購買(Y,繼續(xù) N,退出):");
choose = sc.next();
break;
case 3:
System.out.println("水杯價格為:" + cup + "元,請輸入您需要購買的數(shù)量:");
int s2 = sc.nextInt();
double d2 = s2 * cup;
System.out.println("您購買水杯數(shù)量為:" + s2 + "個,共消費:" + d2 + "元");
total += s2;
Total_price += d2;
System.out.println("是否繼續(xù)購買(Y,繼續(xù) N,退出):");
choose = sc.next();
break;
case 4:
System.out.println("蘋果價格為:" + apple + "元,請輸入需要購買的數(shù)量:");
int s3 = sc.nextInt();
double d3 = s3 * apple;
System.out.println("您購買蘋果數(shù)量為:" + s3 + "個,共消費:" + d3 + "元");
total += s3;
Total_price += d3;
System.out.println("是否繼續(xù)購買(Y,繼續(xù) N,退出):");
choose = sc.next();
break;
case 5:
System.out.println("香蕉價格為:" + banana + "元,請輸入您需要購買的數(shù)量:");
int s4 = sc.nextInt();
double d4 = s4 * banana;
System.out.println("您購買香蕉數(shù)量為:" + s4 + "根,共消費:" + d4 + "元");
total += s4;
Total_price += d4;
System.out.println("是否繼續(xù)購買,需要請輸入Y,否則請輸入N:");
choose = sc.next();
break;
case 6:
System.out.println("QQ糖價格為:" + qqsugar + "元,請輸入您需要購買的數(shù)量:");
int s5 = sc.nextInt();
double d5 = s5 * qqsugar;
System.out.println("您購買QQ糖數(shù)量為:" + s5 + "包,共消費:" + d5 + "元");
total += s5;
Total_price += d5;
System.out.println("是否繼續(xù)購買,需要請輸入Y,否則請輸入N:");
choose = sc.next();
break;
default:
System.out.println("找不到該商品,請重新選擇");
}
}
System.out.println("您一共買了:" + total + "件商品,共消費:" + Total_price + "元");
System.out.println("期待您的再次光臨!");
}
}
2.實驗結(jié)果(包括輸入數(shù)據(jù)和輸出結(jié)果)
文章來源:http://www.zghlxwxcb.cn/news/detail-490944.html
總結(jié)
這個超市小程序到這里就結(jié)束了,還有一個實驗1-2抽取幸運觀眾在主頁有文章來源地址http://www.zghlxwxcb.cn/news/detail-490944.html
到了這里,關(guān)于Java實驗1-1【超市購物小程序】的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!