我对这一切都非常陌生,我第一年主修编程和网页设计。我在网上上课,我的教授有时很难找到。
无论如何,我已经从一个程序中编写了这段代码,该程序获取银行信息(帐号、账户余额和账户类型 [储蓄或支票],计算利息和服务费并输出帐号和账户余额。
我的主要问题是语法错误,指出我的变量“servCharge”可能尚未初始化。我已经看过很多次了,无法追查我做错了什么。
我敢肯定,你们大多数人都很容易发现这样的事情。对此的任何帮助以及有关如何清理代码的总体建议都会有所帮助并非常感谢!
这是我到目前为止所拥有的:
import java.util.*;
public class Unit5
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
final double CHECKING_MIN_BAL = 25.00;
final double CHECKING_SFEE = 25.00;
final double MAX_CHECKING_INTR = .015;
final double MIN_CHECKING_INTR = .03;
final double SAVINGS_MIN_BAL = 500.00;
final double SAVINGS_SFEE = 10.00;
final double SAVINGS_INTR = .04;
final double FIVE_THOU = 5000.00;
String accountNumber;
String accountBal;
String accountType;
double accountBalDbl;
double interest;
double servCharge;
double accountBalFinal;
System.out.println("Bank");
System.out.println("Please enter a 10 digit "
+"account number or enter # to exit: ");
while (! ( accountNumber = console.next() ).equalsIgnoreCase( "#" ) )
{
if (accountNumber.matches("^[0-9]{10}"))
{
System.out.println();
}else{
System.out.println("Invalid account number!");
System.exit(0);
}
System.out.println("Please enter account balance: ");
accountBal = console.next();
{
if (accountBal.matches("^\\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(\\.[0-9][0-9])?$")) //adjust so it takes more numbers before decimal
{
System.out.println();
}else{
System.out.println("Invalid dollar amount!");
System.exit(0);
}
System.out.println("Please enter account type,"
+"C for checking or S for savings");
accountType = console.next();
{
if (accountType.matches("^[Ss | Cc]")) //look into this
{
System.out.println();
}else{
System.out.println("Invalid account type, please enter"
+"C or S");
System.exit(0);
}
accountBalDbl = Double.parseDouble(accountBal);
{
if (accountType == ("[Cc]"))
{
if (accountBalDbl < CHECKING_MIN_BAL)
servCharge = CHECKING_SFEE;
else
servCharge = 0.0;
if (accountBalDbl < CHECKING_MIN_BAL + FIVE_THOU)
interest = accountBalDbl * MIN_CHECKING_INTR;
}
}
{
if (accountType == ("S | s"))
{
if (accountBalDbl < SAVINGS_MIN_BAL)
servCharge = SAVINGS_SFEE;
interest = 0.0;
}else{
servCharge = 0.0;
interest = accountBalDbl * SAVINGS_INTR;
}
}
accountBalFinal = (accountBalDbl - servCharge) + interest;
System.out.println("Account Number: " + accountNumber);
System.out.println();
System.out.println("Current Balance: " + accountBalFinal);
System.out.println();
System.out.println("Please enter a 10 digit "
+"account number or enter # to exit: ");
}
}
}
}
}