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+“个游戏币”);
}
}