绕过初始化变量

时间:2015-12-15 02:43:01

标签: java initialization

我只是搞乱Java代码,看看我能做什么,不能做什么。我遇到了一个我已经创建的String变量的问题,它说它可能没有被初始化。我知道问题是什么以及为什么它可能没有被初始化但是我无法找到如何解决它,我已经尝试将代码放在不同的位置,创建或删除字符串,没有任何作用。这一切都导致了同样的问题。 方法代码:

public class BankAccount {
    private int balance;
    private String name;

    public BankAccount(String n) {
        n = name;
    }

    public void deposit(int dep) {
        balance = balance + dep;
    }

    public void withdrawl(int wit) {
        balance = balance - wit;
    }

    public int getBalance() {
        return balance;
    }
}

测试员代码:

import java.util.Scanner;

public class BankTester {
    public static void main(String[] args) {
        int i = 0;
        int dep;
        int wit;
        int d;
        String dw;
        String alm;

        Scanner input = new Scanner(System.in);

        System.out.print("Name Of Bank Account: ");
        String name = input.next();
        BankAccount account = new BankAccount(name);

        while(true) {
           System.out.println("Open Account? (y/n): ");
           String open = input.next();

           if (open.equals("y")) {
                System.out.print("\f");
                System.out.println("Account Open!");
                break;
           } else if (open.equals("n")) {
                System.exit(1);
           } else {
                System.out.print("\f");
                i++;
                System.out.println("Incorrect Syntax");
                System.out.println("Attempts: " + i);
           }
        }
        while(true) {
            System.out.println();
            System.out.println("Your balance is " + account.getBalance());
            if (account.getBalance() == 0) {
                System.out.print("\f");
                System.out.println("It seems you don't have any money in your account..");
                System.out.print("Would you like to deposit some? (y/n): ");
                alm = input.next();
            } else {
                System.out.print("\f");
                System.out.println("Would you like to do anything with your account " + name + "? (y/n)");
                alm = input.next();
            }
            while (true) {
                if (account.getBalance() > 0) {
                   if (alm.equals("y")) {
                       System.out.print("What would you like to do? (deposit/withdrawl): ");
                       dw = input.next();
                       break;
                   } else if (alm.equals("n")) {
                       System.exit(1);
                   } else {
                       System.out.print("\f");
                       System.out.println("Incorrect Syntax");
                   }
                }
                if (account.getBalance() == 0) {
                    if (alm.equals("y")) {
                       System.out.print("\f");
                       System.out.println("How much money would you like to deposit?");
                       System.out.print("Amount: ");
                       dep = input.nextInt();
                       account.deposit(dep);
                       break;
                   } else if (alm.equals("n")) {
                       System.exit(1);
                   } else {
                       System.out.print("\f");
                       System.out.println("Incorrect Syntax");
                   }
                }
            }
            if (dw.equals("deposit")) {
                System.out.print("\f");
                System.out.println("How much money would you like to deposit?");
                System.out.print("Amount: ");
                dep = input.nextInt();
                account.deposit(dep);
            }
        }
    }
}

它位于底部,表示:

if (dw.equals("deposit")) {
                System.out.print("\f");
                System.out.println("How much money would you like to deposit?");
                System.out.print("Amount: ");
                dep = input.nextInt();
                account.deposit(dep);
            }

"变量dw可能尚未初始化"是我得到的错误。我如何绕过这个if语句,除非余额大于0,否则忽略它?

1 个答案:

答案 0 :(得分:2)

您可以在声明时初始化它:

String dw = "";