我尝试过input = nextLine()并将输入附加到String数据但仍然返回null。这可能是什么问题?
[撤回方法]
public void Withdraw(){
System.out.print("Enter withdrawal amount:" );
double withdraw = input.nextDouble();
System.out.println("Your withdrawal amount: " + withdraw);
if(this.balance - withdraw < 100){
System.out.println("The maintaining balance should not be less than 100. " );
}else
this.balance -= withdraw;
System.out.println("Your new balance is:" + this.balance);
}
[附件文件]
public void withdrawWriter(){
try{
String data = "Your withdrawal amount: " + withdraw;
File file = new File(this.name + ".txt");
//if file doesnt exists, then create it
if(!file.exists()){
file.createNewFile();
}
//true = append file
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
bufferWritter.write("\r\n");
bufferWritter.write(data);
bufferWritter.close();
System.out.println("Done");
}catch(IOException e){
e.printStackTrace();
}
}
以下是截图:
答案 0 :(得分:1)
从屏幕截图中可以清楚地看到,您同时拥有本地变量double withdraw
和类double withdraw
上的字段。
如果你摆脱了本地的话,可能会工作,所以只需:
withdraw = input.nextDouble();
答案 1 :(得分:0)
您正在接受局部变量withdraw中的输入并尝试在withdrawWriter方法中使用实例变量。尝试使用实例变量withdraw,同时接受这样的输入:
public void Withdraw(){
System.out.print("Enter withdrawal amount:" );
**withdraw = input.nextDouble();**
System.out.println("Your withdrawal amount: " + withdraw);
if(this.balance - withdraw < 100){
System.out.println("The maintaining balance should not be less than 100. " );
}else
this.balance -= withdraw;
System.out.println("Your new balance is:" + this.balance);
}