Java---简易自动取货机
生活随笔
收集整理的這篇文章主要介紹了
Java---简易自动取货机
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
已經(jīng)實(shí)現(xiàn)的功能:
源代碼如下:
import java.util.Scanner; public class VendingMachine {int price = 50;//商品單價(jià)int balance;//余額int total;//總金額void showPrompt(){//歡迎信息System.out.println("welcome!");} void insertMoney(){//投入錢Scanner scanner = new Scanner(System.in);System.out.print("請(qǐng)輸入整數(shù): ");int amount = scanner.nextInt();balance = balance + amount;}void showBalance(){//顯示余額System.out.print("當(dāng)前余額為: ");System.out.println(balance);}void getFood(){//獲得商品if( balance >= price){System.out.println("Here you are!");balance = balance - price;total = total + price;}}void peroration(){//結(jié)束語System.out.println("Looking forward to your next visit!");}public static void main(String[] args) {// TODO Auto-generated method stubVendingMachine vm = new VendingMachine();vm.showPrompt();vm.showBalance();vm.insertMoney();vm.showBalance();vm.getFood();vm.showBalance();vm.peroration();} }總結(jié)
以上是生活随笔為你收集整理的Java---简易自动取货机的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android ConstraintLa
- 下一篇: Java---中国有句俗语叫“三天打鱼两