有一个换币机器,可以投1元,5元,10元的人民币。最后按一下出货按钮,可以吐出游戏币。(每个游戏币=1元人民币)

package my;

public class Example

{
public int money=0;//机器里投入多少钱
public void insertCash(int cash)
{
if(cash>10)
{
System.out.println(“只能识别1元,5元,10元!”);
return;
}
this.money += cash;
System.out.println(“当前余额为:”+cash);
}
//
public int exchange()

{
	int numOfCoin=this.money/1;
	this.money=0;
	System.out.println("交易完成,当前余额"+this.money);
	return numOfCoin;
}

}
调用
package my;

public class HelloWorld
{
public static void main(String[]args)
{
Example m=new Example();
m.insertCash(5);
m.insertCash(10);
m.insertCash(50);
int coins=m.exchange();
System.out.println(“拿到了”+coins+“个游戏币”);
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值