跪谢!求用JAVA编写一个自动贩卖机的程序

用JAVA编写一个自动贩卖机的程序,要求提供咖啡,可乐和茶,三种饮料。价格分别为2元,3元和5元。顾客选择自己想要的东西后,投币,然后系统还要找补零钱。谢谢!

import java.util.Scanner;

public class Shop
{
private String coffee="咖啡";
private String coke="可乐";
private String tea="茶";
private double coffeeprice=2.00;
private double cokeprice=3.00;
private double teaprice=5;
private double price=0.0;
public Shop()
{
}
public void getName(int i)
{
if(i==1)
{
System.out.println("你选择的是咖啡");
}

if(i==2)
{
System.out.println("你选择的是可乐");
}

if(i==3)
{
System.out.println("你选择的是茶");
}
}
public double getShopping(int i,double p)
{
if(i==1)
{
if(p<coffeeprice)
{
System.out.println("您的钱不够买此商品");
}
else
{
price=p-coffeeprice;
}
}

if(i==2)
{
if(p<cokeprice)
{
System.out.println("您的钱不够买此商品");
}
else
{
price=p-cokeprice;
}
}

if(i==3)
{
if(p<teaprice)
{
System.out.println("您的钱不够买此商品");
}
else
{
price=p-teaprice;
}
}

return price;
}

public static void main(String[] args)
{
Shop shop=new Shop();
try
{
System.out.println("请选择你想要的商品\n 1.咖啡 2.可乐 3.茶");
Scanner si=new Scanner(System.in);
int i=si.nextInt();
System.out.println(" ");
if(i>3||i<1)
{
System.out.println("没有此产品");
}
else
{
shop.getName(i);
System.out.println(" ");
System.out.print("请付账:");
Scanner sp=new Scanner(System.in);
double p=sp.nextDouble();
System.out.println(" ");
if(shop.getShopping(i,p)!=0.0)
{
System.out.println("找你的钱:"+shop.getShopping(i,p));
}
}
}
catch(Exception ex)
{
System.out.println("请正确输入!");
}
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-12
package mym3;

/*
* 用JAVA编写一个自动贩卖机的程序,要求提供咖啡,可乐和
* 茶,三种饮料。价格分别为2元,3元和5元。顾客选择自己想
* 要的东西后,投币,然后系统还要找补零钱。谢谢!
*/
import java.util.*;

public class crs {
public static void main(String[] agse) {
int a = 0, b = 0, c = 0, aa = 0;
Scanner input = new Scanner(System.in);
System.out.println("请选择物品的代号:1咖啡 2可乐 3茶");
String as = input.next();
if (as.equals("1")) {
System.out.println("你要买的是茶:2元");
a = 1;
} else if (as.equals("2")) {
System.out.println("你要买的是茶:3元");
b = 1;
} else if (as.equals("3")) {
System.out.println("你要买的是茶:5元");
c = 1;
} else {
System.out.println("你的输入有误!请重新输入!");
}
System.out.println("请支付现金:");
int as1 = input.nextInt();
if(as1>=2){
System.out.println("成了");
aa = as1 - 2;
}else if(as1>=3){
System.out.println("成了");
aa = as1 - 3;
}else if(as1>5){
System.out.println("成功");
aa = as1 -5;
}else{
System.out.println("你的钱不够");
}
if(aa==0){
System.out.println("你的钱刚好");
}else{
System.out.println("找你的钱为:"+aa);
}
}
}
相似回答