java题求助

java题求助,第1张

public class AccountBookTest {

public static void main(String[] args) {

// TODO Auto-generated method stub

AccountBook account = new AccountBook("张三",30,10)

System.out.println("账户名称:" + account.accountName + "收入额:" + account.income + "支出额:" + account.outcome + "余额:" + account.compute(account.income, account.outcome))

}

}

class AccountBook{

String accountName

double income

double outcome

AccountBook(String accountName,double income,double outcome)

{

this.accountName = accountName

if(income >0)

{

this.income = income

}

else{

System.out.println("收入不能为负数!")

}

if(outcome >0)

{

this.outcome = outcome

}

else{

System.out.println("支出不能为负数!")

}

}

public double compute(double income,double outcome)

{

double balance = 0.0

balance = income - outcome

return balance

}

}

代码如下:

import java.io.*

import java.awt.*

import java.awt.event.*

public class jtxtfm{

public static void main(String args[]){

jtxtfrm fm=new jtxtfrm()

}

}

class jtxtfrm extends Frame implements ActionListener{

FileDialog op,sv

Button btn1,btn2,btn3

TextArea tarea

jtxtfrm(){

super("读写文件")

setLayout(null)

setBackground(Color.cyan)

setSize(600,300)

setVisible(true)

btn1=new Button("打开")

btn2=new Button("保存")

btn3=new Button("关闭")

tarea=new TextArea("")

add(btn1)add(btn2)add(btn3)add(tarea)

tarea.setBounds(30,50,460,220)

btn1.setBounds(520,60,50,30)

btn2.setBounds(520,120,50,30)

btn3.setBounds(520,180,50,30)

op=new FileDialog(this,"打开",FileDialog.LOAD)

sv=new FileDialog(this,"保存",FileDialog.SAVE)

btn1.addActionListener(this)

btn2.addActionListener(this)

btn3.addActionListener(this)

    addWindowListener(

     new WindowAdapter(){

      public void windowClosing(WindowEvent e){

       setVisible(false)

       System.exit(0)

      }

     }    

    )

}

public void actionPerformed(ActionEvent e){

if(e.getSource()==btn1){

String str

op.setVisible(true)

try{

File f1=new File(op.getDirectory(),op.getFile())

FileReader fr=new FileReader(f1)

BufferedReader br=new BufferedReader(fr)

tarea.setText("")

while((str=br.readLine())!=null)tarea.append(str+'\n')

fr.close()

}

catch(Exception e1)

{}

}

if(e.getSource()==btn2){

sv.setVisible(true)

try{

File f1=new File(sv.getDirectory(),sv.getFile())

FileWriter fw=new FileWriter(f1)

BufferedWriter bw=new BufferedWriter(fw)

String gt=tarea.getText()

bw.write(gt,0,gt.length())

bw.flush()

fw.close()

}

catch ( Exception e2)

{}

}

if(e.getSource()==btn3){

System.exit(0)

}

}

}

效果图:

import java.util.Scanner

public class SavingAccount {

public static double account = 3000

public static double rate = 0.03

public void update(double rate){

this.rate = rate

}

//flag 用来计算年利息和月利息,flag等true计算月利息

public double calc(double account , double rate , boolean flag){

if(flag)

return account * rate / 12

return account * rate

}

static class Test{

public static void main(String[] args) {

System.out.println("原始存款:"+SavingAccount.account + " \n原始利率:" +SavingAccount.rate )

SavingAccount sa = new SavingAccount()

System.out.println("原始年利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,false))

System.out.println("原始月利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,true))

System.out.println("######请输入新的利率######")

Scanner scanner = new Scanner(System.in)

sa.update(scanner.nextDouble())

System.out.println("更新后的利率:"+SavingAccount.rate)

System.out.println("更新后的年利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,false))

System.out.println("更新后的月利息:"+sa.calc(SavingAccount.account,SavingAccount.rate,true))

//更新后的存款怎么算? 你是多长时间更新一次?

System.out.println("更新后的存款:"+(SavingAccount.account+sa.calc(SavingAccount.account,SavingAccount.rate,false)))

}

}

}

不知道对不对,我理解的是这样的了


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11591778.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存