简易自助售货机(JAVA)

用JAVA面向对象编程方式编的(搬运的)

这是构想图,上面是呈现给顾客的功能

下面是人为设置的变量

price:食品价格

amount:投入货币面值

balance:用户账户余额

total:售货机总收入

 代码:

package vendingmachine;

public class VendingMachine {
    int price = 80;
    int balance;
    int total;
    
    void showPrompt() {//欢迎语
        
        System.out.println("Welcome!");
    }
    
    void inserMomey(int amount) {//投币口
        balance = balance + amount;
    }
    
    void showBalance() {//用户余额显示
        System.out.println(balance);
    }
    
    void getFood() {//出货口
        if(balance >= price) {
            balance = balance - price;
            System.out.println("Here you are");
            total = total + price;
        }
    }
    
    public static void main(String[] args) 
    {
        VendingMachine vm = new VendingMachine();
        vm.showPrompt();
        vm.showBalance();
        vm.inserMomey(100);
        vm.getFood();
        vm.showBalance();
        
    }

}
View Code

输出结果:

PS:先想自己要达到什么目的,然后再去想细节,这样你更能清楚自己想做什么,要做什么,一步步来,不着急!

小记(编代码的时候想起了一些伤感的事,想起了自己之前很喜欢的一个女生,很郁闷,也很烦躁,一想到她可能有其他可能了,就更难受;可能打代码、上上课能让我有所解闷吧,一会儿再听听歌吧,折磨自己很难受,为了她也不值得,女人嘛,只会影响你出剑的速度!有代码,有机械键盘陪伴你,你还郁闷个什么劲啊!)

原文地址:https://www.cnblogs.com/yeluozhiqiumax/p/15228386.html