【发布时间】:2015-10-20 14:41:26
【问题描述】:
现在我正在处理一个ATM 项目。
用户将输入他们的姓名、余额、交易类型和交易金额。交易类型为Withdraw (W)、Deposit (D)和R (Report),我用的是switch,有3个casesW、D、R。如果您出现以下情况,程序将输出错误: 输入错误代码,您没有足够的资金来进行您选择的交易类型,并且您输入的金额为负数。现在我很难设置switch。我的代码如下,任何帮助将不胜感激!谢谢
import java.io.*;
import java.util.*;
public class atmValidator {
public static void main(String[] args) {
String fname = " ", lname = " ";
double balbefore = 0.0, balafter = 0.0, transamount = 0.0;
char code = ' ';
Scanner screen = null;
FileInputStream fis = null;
FileOutputStream fos = null;
PrintWriter pw = null;
//Declaring the variables/scanner/etc.
try {
fis = new FileInputStream ("transactions.txt");
fos = new FileOutputStream ("statement.txt");
screen = new Scanner (fis);
pw = new PrintWriter (fos);
} catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}
//The try/catch for reading from the file
while (screen.hasNext()) {
fname = screen.next();
lname = screen.next();
balbefore = screen.nextDouble();
code = screen.next().charAt(0);
transamount = screen.nextInt();
//A while loop to gather the info from the file, and the variables getting their values from the files.
switch (code) {
case 'D':
balafter = balbefore + transamount;
if (transamount <0) {
pw.println("ERROR: Enter positive amount.");
}
else if (balafter < 300) {
pw.println("Warning, balance below $300");
}
{
// Where I am stuck at :(
}
}
}
screen.close();
pw.close();
}
}
//Sorry for the weird braces and spaces
【问题讨论】:
标签: java if-statement switch-statement logic