十四章上機練習四
阿新 • • 發佈:2019-01-31
import java.util.*; public class Goods { String[] goods=new String[]{"電風扇","洗衣機","電視機","冰箱","空調機"}; double[] price=new double[]{124.23,4500,8800.90,5000.88,4456,1200.46}; double total; public boolean login(){ boolean flag=false; Scanner input=new Scanner(System.in); System.out.print("請輸入使用者名稱: "); String name=input.next(); System.out.print("請輸入密碼: "); String pwd=input.next(); if(name.equals("Tom")&&pwd.equals("123")){ System.out.println("登入成功"); flag=true; }else{ System.out.println("請重新登入"); } return flag; } public StringBuffer change(double d){ StringBuffer str=new StringBuffer(String.valueOf(d)); for(int i=str.indexOf(".")-3;i>0;i=i-3){ str.insert(i, ','); } return str; } public void show(){ Scanner input=new Scanner(System.in); System.out.println("*********歡迎進入商品批發城*********"); System.out.println("\t編號\t商品\t價格"); for(int i=0;i<goods.length;i++){ System.out.println("\t"+(i+1)+"\t"+goods[i]+"\t"+change(price[i])); } System.out.println("************************************"); System.out.print("請輸入您批發的商品編號: "); int choice=input.nextInt(); System.out.print("請輸入批發數量: "); int number=input.nextInt(); switch(choice){ case 1: total=price[0]*number; break; case 2: total=price[1]*number; break; case 3: total=price[2]*number; break; case 4: total=price[3]*number; break; case 5: total=price[4]*number; break; } System.out.println("您需要付款:"+change(total)); } public static void main(String[] args) { Goods g=new Goods(); g.login(); g.show(); } }