**今天给大家带来一个简单的售货机程序。**
主要流程:
1.利用数组实现相关商品的存储和价格。
2.利用三个函数:
(1)menu():主菜单,用于实现商品的展示和基本选择。
(2)getnumber():获得商品的数量,并且计算价格。
(3)huode():输入你个人的金额,并且判断是否满足商品的总价 格,若满足,则计算找零价格。并且选择返回主界面或者是退出,若不满足,根据用户选择是否决定重新输入产品质量或者是返回主界面或者是退出界面。
public void huode(int choice)//计算找零
{
float thingprice;//商品总价格
int nideshuru;//你输入钱数
float jieguo;//找零数目
int choice1;//返回主界面或者退出的选择
thingprice=getnumber(choice);
System.out.println("请输入你的金额:");
Scanner s=new Scanner(System.in);
nideshuru=s.nextInt();
while(nideshuru<0||nideshuru>100)
{
System.out.println("你的输入格式错误");
System.out.println("请重新输入:");
nideshuru=s.nextInt();
}//判断顾客输入价格是否满足条件
if(nideshuru>thingprice)
{
jieguo=nideshuru-thingprice;
System.out.printf("找零金额:%.2f",jieguo);
System.out.println("是否返回页面?1.是/n2.不是(任意键)");
choice1=s.nextInt();
if(choice1==1)
{charrr.menu();
}
else
System.exit(0);
}
else
{System.out.println("你的钱不够!");//设置重新选择商品数量,返回界面,退出。
System.out.println("是否重新选择?1.改变购买数量。/n2.返回主界面。/n3.按任意键退出。");
choice1=s.nextInt();
if(choice1==1)
{charrr.getnumber(choice1);
this.huode(choice);}
if(choice1==2)
charrr.menu();
else
System.exit(0);}}
public static void main(String[] args) {//调用主菜单函数
charrr.menu();}}