自动售货机的java代码 自动售货机源代码
急!java的自动售货机找零程序
public class Change {
成都创新互联主营下陆网站建设的网络公司,主营网站建设方案,成都app软件开发公司,下陆h5微信小程序定制开发搭建,下陆网站营销推广欢迎下陆等地区企业咨询
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.print("The goods cost:");
int cost=sc.nextInt();
System.out.print("The customer paid:");
int paid=sc.nextInt();
change(cost,paid);
}
public static void change(int cost,int paid){
//unit of costpaid is dollar
int change=(paid-cost)*100;
int numOfTF=0;//number of 25 cents
int numOfT=0;//number of 10 cents
int numOfF=0;//number of 5 cents
if(change0)//payment is not enough
System.out.println("payment is not enough");
else{
numOfTF=(int)change/25;
numOfT=(int)(change-numOfTF*25)/10;
numOfF=(int)(change-numOfTF*25-numOfT*10)/5;
System.out.println("Change is: "+numOfTF+" 25 cents, "+numOfT+" 10 cents, "+numOfF+" 5 cents.");
}
}
}
能用JAVA编写一个自动贩卖机的程序吗?
importjava.util.Scanner;\x0d\x0a\x0d\x0apublicclassShop\x0d\x0a{\x0d\x0aprivateStringcoffee="咖啡";\x0d\x0aprivateStringcoke="可乐";\x0d\x0aprivateStringtea="茶";\x0d\x0aprivatedoublecoffeeprice=2.00;\x0d\x0aprivatedoublecokeprice=3.00;\x0d\x0aprivatedoubleteaprice=5;\x0d\x0aprivatedoubleprice=0.0;\x0d\x0apublicShop()\x0d\x0a{\x0d\x0a}\x0d\x0apublicvoidgetName(inti)\x0d\x0a{\x0d\x0aif(i==1)\x0d\x0a{\x0d\x0aSystem.out.println("你选择的是咖啡");\x0d\x0a}\x0d\x0a\x0d\x0aif(i==2)\x0d\x0a{\x0d\x0aSystem.out.println("你选择的是可乐");\x0d\x0a}\x0d\x0a\x0d\x0aif(i==3)\x0d\x0a{\x0d\x0aSystem.out.println("你选择的是茶");\x0d\x0a}\x0d\x0a}\x0d\x0apublicdoublegetShopping(inti,doublep)\x0d\x0a{\x0d\x0aif(i==1)\x0d\x0a{\x0d\x0aif(p
跪谢!求用JAVA编写一个自动贩卖机的程序
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(pcoffeeprice)
{
System.out.println("您的钱不够买此商品");
}
else
{
price=p-coffeeprice;
}
}
if(i==2)
{
if(pcokeprice)
{
System.out.println("您的钱不够买此商品");
}
else
{
price=p-cokeprice;
}
}
if(i==3)
{
if(pteaprice)
{
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(i3||i1)
{
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("请正确输入!");
}
}
}
求一个简易自动售货机的代码(java)要用创建类封装性,输出的时候要有提示语句,代码类似以下图片
//Example类文件Example.java
package cn.zhouhan;
import java.util.Scanner;
public class Example {
static Drink cola, fanta, blacktea, greentea;
static int money;
public static void main(String[] args) {
// TODO Auto-generated method stub
cola = new Drink("可乐", 50, 2.0, 1);
fanta = new Drink("芬达", 40, 3.0, 2);
blacktea = new Drink("红茶", 30, 4.0, 3);
greentea = new Drink("绿茶", 20, 5.0, 4);
money = 500;
for()
{
System.out.println("-------------自动售货机-------------");
cola.UserPrintDrinkInfo();
fanta.UserPrintDrinkInfo();
blacktea.UserPrintDrinkInfo();
greentea.UserPrintDrinkInfo();
System.out.println("----------------------------------");
System.out.println("1.购买饮料");
System.out.println("2.管理员查询");
System.out.println("3.返回");
System.out.println("请输入要执行的操作序号:");
Scanner scannerIndex = new Scanner(System.in);
int indexScanner = scannerIndex.nextInt();
switch(indexScanner)
{
case 1:
System.out.println("请选择购买的饮料(1.可乐,2.芬达,3红茶,4.绿茶):");
Scanner numberScanner = new Scanner(System.in);
int buyIndex = numberScanner.nextInt();
Drink drink = getDrink(buyIndex);
System.out.println("请输入购买的数量:");
Scanner countScanner = new Scanner(System.in);
int buyCount = countScanner.nextInt();
if (buyCount 0)
{
System.out.println("您选择了" + buyCount + "瓶" + drink.getName() + "." );
System.out.println("总价格:" + drink.getPrice() * buyCount + "元,请投入1元或5元纸币。");
System.out.println("确定购买?(1.确定,2.退款):");
Scanner sureScanner = new Scanner(System.in);
int sure = sureScanner.nextInt();
if (sure == 1)
{
drink.sellDrink(buyCount);
money += drink.getPrice() * buyCount;
System.out.println("您购买了" + buyCount + "瓶" + drink.getName() + ",交易成功." );
}
if (sure ==2 )
{
System.out.println("退款成功!");
}
}
else
System.out.println("输入的数量有误!");
break;
case 2:
System.out.println("请输入管理员密码:");
Scanner pwScanner = new Scanner(System.in);
int password = pwScanner.nextInt();
if (password == 123)
{
System.out.println("密码正确,目前的余额为:" + money + "元。");
}
else
System.out.println("密码错误。");
break;
case 3:
break;
default:
System.out.println("请输入正确的数字。");
break;
}
}
}
public static Drink getDrink(int drinkIndex)
{
Drink drink = null;
switch(drinkIndex)
{
case 1:
drink = cola;
System.out.println("您选择了可乐。");
break;
case 2:
drink = fanta;
System.out.println("您选择了芬达。");
break;
case 3:
drink = blacktea;
System.out.println("您选择了红茶。");
break;
case 4:
drink = greentea;
System.out.println("您选择了绿茶。");
break;
default:
//drink = null;
System.out.println("输入错误,请输入1~4的数字!");
}
return drink;
}
}
//创建的Drink类文件Drink.java
package cn.zhouhan;
public class Drink {
private String name;
private int number;
private double price;
private double drinkMoney;
private int index;
public Drink(String name, int number, double price, int index)
{
this.name = name;
this.number = number;
this.price = price;
this.drinkMoney = number * price;
this.index = index;
}
public String getName()
{
return this.name;
}
public int getNumber()
{
return this.number;
}
public double getPrice()
{
return this.price;
}
public void sellDrink(int count)
{
this.number -= count;
this.drinkMoney += this.price * count;
}
public void UserPrintDrinkInfo()
{
System.out.println("品名:"+ this.name +",单价:" + this.price + "元,剩余数量:" + this.number + "瓶。");
}
}
当前题目:自动售货机的java代码 自动售货机源代码
文章链接:http://cdiso.cn/article/ddjdjcj.html