有一个换币机器,可以投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+“个游戏币”);
}
}

猜你喜欢

转载自blog.csdn.net/weixin_43398418/article/details/86793271