
if(ae.getSource()==btn0){
sb.append('0')
jtfDisplay.setText(sb.toString())
}
else if(ae.getSource()==btn[0]){
sb.append('1')
jtfDisplay.setText(sb.toString())
}
else if(ae.getSource()==btn[1]){
sb.append('2')
jtfDisplay.setText(sb.toString())
}
}
你这里有一个嵌套,但是按钮的事件监听是不能进行嵌套的,因为它每次判别过后结果不保存,下一次重新调用事件监听器判断,不可能执行到嵌套内部的ae.getSource()==btn0之类的代码.就没有结果.
代码还有一个问题就是,如果你在清除的时候没有把字符串中内容清除掉,再调用这个字符串的话它里面的内容还在,就不能达到预期的目的.
import java.util.Scannerpublic class PhoneCard {
private final int id // 卡号
private final int password //密码
private double cash //剩余金额
private double cashPerMin = 0.2// 每分钟收费
private Scanner sc = new Scanner(System.in)
public PhoneCard(int id,int password,double cash) {
this.id = id
this.password = password
this.cash = cash
}
/**
* 拨号计费
* @param time 拨号时间 单位分钟
* 返回剩余金额
*/
public double callSomeone(double time) {
System.out.println("请输入卡号和密码,用空格隔开")
int id = sc.nextInt()
int pw = sc.nextInt()
if(this.id == id && this.password == pw) {
this.cash = this.cash - (double)time * cashPerMin
return cash
} else {
System.out.println("密码错误")
return -1
}
}
}
public class TestPhoneCard {
public static void main(String[] args) {
PhoneCard pc = new PhoneCard(1, 1, 100) //初始化,卡号,密码,余额
double money = pc.callSomeone(10) //拨打10分钟
System.out.println("余额为" + money + "元")
money = pc.callSomeone(90) //继续拨打
System.out.println("余额为" + money + "元")
}
}
不知道为什么没人做
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)