题主悬赏了10铜    |    已采纳 yes_rpg 的答案

有木有会java的大佬 解惑一段很简单的代码

微博   微信 2017-10-17 21:32     8个回答 求码
编了一段代码 语法没有问题 但是程序运行到红色代码的那一行就运行不下去了 找不到问题所在 求问大佬解惑

import FormatIO.*;
public class part1 {
public static void main(String[] arg)
{
Console con = new Console();
con.print("Creat new account");
con.println("Please enter your first account number:");
String accountNumber = con.readLine();
con.println("Please enter your initial balance:");
double initialBalance = con.readDouble();
String fileName = "account-"+accountNumber+".txt";
FileOut fout = new FileOut(fileName);
String accountLine = accountNumber+" "+initialBalance;
fout.print(accountLine);
con.println("Account file <"+fileName+"> written") ;
fout.close();

con.println("Please enter your second account number:");
accountNumber = con.readLine();
con.println("Please enter your initial balance:");
initialBalance = con.readDouble();
fileName = "account-"+accountNumber+".txt";
fout = new FileOut(fileName);
accountLine = accountNumber+""+initialBalance;
fout.print(accountLine);
con.println("Account file <"+fileName+"> written") ;
fout.close();
/*
* Step1 is completed
*/

con.print("Please enter the number of your apartment rental months to be paid:");
int months = con.readInt();
con.println("Please enter the rent per month:");
double perRent = con.readDouble();
double rent = months*perRent;
con.println(String.format("The total rent that you need to pay is %.2f", rent));
con.println("Please enter your bank account number:");
accountNumber = con.readLine();
fileName = "account-"+accountNumber+".txt";
FileIn fIn = new FileIn(fileName);
accountNumber = fIn.readWord();
double balance = fIn.readDouble();
fIn.close();
balance = balance-rent;
fout = new FileOut(fileName);
accountLine = accountNumber+" "+balance;
fout.print(accountLine);
con.println("successful payment");
fout.close();

}
}
回答
  • 在p9你甚至可以讨论java
    2017-10-17 21:47
  • 本条答案已被采纳 yes_rpg
    double balance = Double.valueOf(acountNumber+“”);
    2017-10-17 21:51
    •    2017-10-17 21:57 回复 yetwong  @yes_rpg 感谢大佬 已经可以运行了 但是还是有点不明所以 可以讲解一下原因么?感谢感谢
    •    2017-10-17 22:00 回复 层主  @yetwong fileName你把它加了头和尾的字符串 xx.txt的格式不管你转成什么类型 这类型都不可能转成数字的
  • @yes_rpg 很感谢回复 但是我看了答案 fileName的格式(红色部分)跟我的是一样的 但是这一整串代码是可以运行的

    import FormatIO.*;

    public class Lab5 {

    public static void main(String[] arg)
    {

    Console con = new Console("Open New Bank Accounts") ;
    con.print("Enter first bank account number: ") ;
    String accountNumber = con.readLine() ;
    con.print("Enter initial balance of account "+accountNumber+": £") ;
    double initialDeposit = con.readDouble() ;
    String fileName = "account-"+accountNumber+".txt" ;
    String accountLine = accountNumber+" "+initialDeposit ;
    FileOut fout = new FileOut(fileName) ;
    fout.println(accountLine) ;
    con.println("Account file <"+fileName+"> written") ;
    fout.close() ;
    con.print("\nEnter second bank account number: ") ;
    accountNumber = con.readLine() ;
    con.print("Enter initial balance of account "+accountNumber+": £") ;
    initialDeposit = con.readDouble() ;
    fileName = "account-"+accountNumber+".txt" ;
    accountLine = accountNumber+" "+initialDeposit ;
    fout = new FileOut(fileName) ;
    fout.println(accountLine) ;
    con.println("Account file <"+fileName+"> written") ;
    fout.close() ;

    con.println("\n\nPayment") ;
    con.print("Enter number of apartment rental months to be paid: ") ;
    int months = con.readInt() ;
    con.print("Enter the monthly price of the apartment: £") ;
    double rental = con.readDouble() ;
    double price = rental * months ;
    con.println(String.format("The total price is £%.2f", price)) ;
    con.print("Enter account number to be debited: ") ;
    accountNumber = con.readLine() ;

    fileName = "account-"+accountNumber+".txt" ;
    FileIn fin = new FileIn(fileName) ;

    accountNumber = fin.readWord();
    double balance = fin.readDouble();


    fin.close() ;

    balance = balance - price ;
    accountLine = accountNumber+" "+balance ;

    fout = new FileOut(fileName) ;
    fout.println(accountLine) ;
    fout.close() ;

    con.println("Account file <"+fileName+"> written") ;
    con.println(String.format("The new balance of account %s is £%.2f",accountNumber,balance)) ;
    }
    }
    2017-10-17 22:09
  • public static void main(String[] arg)
    {
    Console con = new Console();
    con.print("在p9你甚至能讨论游戏");
    }
    2017-10-18 10:21修改
  • 在p9你甚至能讨论游戏系列
    2017-10-18 10:37
发表评论,请先 登录
T