我对这一切都非常陌生,我在编程和网页设计专业的第一年。我在线上课程,有时很难找到教授。

无论如何,我已经从一个使用银行信息(帐号,帐户余额和帐户类型[储蓄或支票],计算利息和服务费并输出帐号和帐户余额的程序)中编写了此代码。

我的主要问题是语法错误,指出我的变量“ 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: ");

         }
        }
      }
    }
  }

最佳答案

格式化需要一些技巧,但是您提到的错误与以下事实有关:您的代码中至少有一个分支,您没有为servCharge分配值。当您尝试这样做时,这是一个问题:

accountBalFinal = (accountBalDbl - servCharge) + interest;


确保始终通过所有分支将servCharge设置为某种值,并且/或者确保在代码开始时将其设置为默认值。就像是:

servCharge = 0.0;


这样做的最大问题是,您最终可能会得到意想不到的servCharge值(0.0)。检查所有if / else分支,并确保为每个分支中的servCharge分配了适当的值。

09-27 03:34