Java 简单的小编程求大神来解答一下4
Createaprogramthatreadsaninitialamounttobeplacedinanaccountthenreadstendepositsusinga...
Create a program that reads an initial amount to be placed in an account then reads ten deposits using a method that prints a nice text telling the costumer what to do, reads the amount and then returns it. After each deposit a second method shall be called that takes the read amount and adds a 10% bonus before the amount is returned and deposited to the account.
Feel free to print explaining text for each step. Print the final amount stored in the account to the screen.
Must use at least: 1. One Scanner 2. One method without parameters that returns a value 3. One method taking one parameter and returning a value 展开
Feel free to print explaining text for each step. Print the final amount stored in the account to the screen.
Must use at least: 1. One Scanner 2. One method without parameters that returns a value 3. One method taking one parameter and returning a value 展开
展开全部
理解的可能有偏差,如有错误请追问
创建一个程序,读取被放置在一个帐户的金额。账户的初始存款是10,打印好文字告诉客户怎么做,创建账户后然后返回账户余额。每一次存款后,返回余额,在该金额被返回并存入该帐户之前增加10%。
随意打印文字说明每一步。打印到屏幕上的最后一笔帐户。
至少要使用:1。Scanner类的相关方法2。没有参数的一个方法,该方法返回一个值3。一个带一个参数的方法,该方法返回一个值。
考察的点:
1、scanner工具类使用
2、方法重载
3、文件读写
请注意,下面的代码仅供参考,用以提示思路,不能作为试题答案使用。
但是相关问题的使用方法都做了代码示例。
package source;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class AccountReader {
private static final String filepath = "C:\\Users\\admin\\Desktop\\account.txt";
/**创建初始存款为10.0的账户*/
public static void createAccount() throws IOException{
createAccount(10.0);
}
/**创建初始存款为任意数字的账户*/
public static void createAccount(double val) throws IOException{
File file = new File(filepath);
if(!file.exists()){
if(!file.getParentFile().exists()){
file.getParentFile().mkdirs();
}
file.createNewFile();
}
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file));
osw.write(String.valueOf(val));
osw.close();
}
public static double readAccount() throws FileNotFoundException{
double val = 0.0;
val = read();
return val;
}
public static double saveAccount(double val) throws IOException{
double r_val = 0.0;
double rate = 0.1;
r_val = read();
double s_val = r_val;
s_val = r_val * (1 + rate) + val;
save(s_val);
return s_val;
}
/**读取账户文件*/
private static double read() throws FileNotFoundException{
Scanner sc = new Scanner(new File(filepath));
double val = sc.nextDouble();
sc.close();
return val;
}
private static void save(double val) throws IOException{
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(filepath));
osw.write(String.valueOf(val));
osw.close();
}
public static void main(String[] args){
try {
createAccount();
System.out.println(readAccount());
System.out.println(saveAccount(50.0));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
有疑问或者错误,请追问
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询