我對所有這些都非常陌生,我的第一年主修編程和網頁設計。我在網上參加我的課程,我的教授偶爾很難得到。變量可能未初始化錯誤和一般建議
反正我已經寫了從一個程序,它的銀行信息(賬號,賬戶餘額和賬戶類型[儲蓄或checkings],計算利息和服務費,並輸出賬戶號碼和賬戶餘額的代碼。
我的主要問題是一個語法錯誤,指出我的變量「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: ");
}
}
}
}
}