我基本上在应用程序中制作了一款名为BlueJ的手机。它进入电话通话的持续时间并插入信用卡。每分钟等于一磅。所以基本上如果我插入5磅和6分钟,它将做6-5。
在这种情况下,它无法正常工作。
信用证成功,但随着信用的变化,持续时间会从新信用卡中减去,而不是输入的信用卡。
有没有办法绕过这个或同时运行它的方法,或旧的值在变化之前?
问题出在哪里:
credit = credit - duration;
duration = duration - credit;
非常感谢。
public void makePhoneCall()
{
if(credit == 0)
System.out.println("Insert more than 0 credit to make a phone call!");
else {
if(credit >= duration) {
System.out.println("The phone number " + number + " is being dialed for " + duration + " minutes");
credit = credit - duration;
duration = duration - credit;
}
else {
if(credit < duration)
System.out.println("You do not have enough credit to make a phone call! Your credit is " + credit + " pounds");
}
}
答案 0 :(得分:0)
我不确定我是否得到了你想做的事,但这就是我所理解的:
使用credit = credit - duration;
,您正在重新定义&#34; credit&#34;的值。但是你想减去&#34; credit&#34;的原始价值。在下面的行。
您可以在将值修改为另一个变量之前简单地存储该值,如下所示:
int oldCredit = credit;
credit = oldCredit - duration;
duration = duration - oldCredit;