为什么我的其他声明不起作用?

时间:2016-03-13 22:31:27

标签: java if-statement

对于我很好奇的事情,我非常直截了当。我不明白为什么当我输入的金额等于0或小于提款时,我没有得到我在课堂上的错误信息。

代码在这里:

Mainclass:

import java.text.NumberFormat;

public class Account {

private final double RATE = 0.03; // interest rate of 3%

private String customer;
private int id;
private long account_id;
public double balance;

// Creates new account with specified information
public Account(int id, String name, long acctNum, double acctBalance) {
    customer = name;
    account_id = acctNum;
    balance = acctBalance;

}

public double deposit(double amount) {
    // If deposit is larger than 0 increase by deposit amount
    if (amount > 0)
        balance = balance + amount;

    else {
        System.out.println("Invalid deposit amount");
    }

    return balance;
}

public double withdraw(double amount) {
    // If withdrawal is larger than 0 reduce balance by amount
    if (amount <= balance || amount > 0)
        balance = balance - amount;

    else {
        System.out.println("Invalid withdrawal amount or insufficient funds");
    }

    return balance;
}

// Method to add interest to account
public double addInterest() {
    balance += (balance * RATE);
    return balance;
}

public double getBalance() {
    return balance;
}

@Override
public String toString() {

    NumberFormat fmt = NumberFormat.getCurrencyInstance();

    return (customer + "\t" + account_id + "\t" + fmt.format(balance));
}

}

驱动程序类:

import java.util.Scanner;

public class AccountManager {

public static void main(String[] args) {
    // Create array space for 30 accounts

    String customer;
    long account_id;
    double balance;
    int id = 0;

    int count = 0;
    int amount = 0;

    Account[] account_array = new Account[30];

    Scanner scan = new Scanner(System.in);

    String input = "";

    System.out.println("Would you like to create an account?");
    System.out.println("Type 'yes' to add an account");
    input = scan.nextLine();

    while (input.equals("yes")) {

        System.out.println("To create an account please enter the following: ");

        System.out.println("ID number (0,1,2,3..etc)");
        id = scan.nextInt();
        scan.nextLine();

        System.out.println("Name: ");
        customer = scan.nextLine();

        System.out.println("Account Number: ");
        account_id = scan.nextLong();

        System.out.println("Current Balance: ");
        balance = scan.nextDouble();
        scan.nextLine();

        account_array[count] = new Account(id, customer, account_id, balance);

        count++;

        System.out.println("Would you like to create another account?");
        System.out.println("Type 'yes' to add another account");
        input = "";
        input = scan.nextLine();

    }

    System.out.println("Enter the ID of the account you would like to make changes to: ");

    id = scan.nextInt();
    scan.nextLine();

    System.out.println("Would you like to deposit or withdraw cash? ");
    input = scan.nextLine();

    if (input.equals("withdraw")) {

        System.out.println("How much would you like to withdraw?");

        amount = scan.nextInt();
        account_array[id].withdraw(amount);
        System.out.println("Balance for account: " + id + " = " + account_array[id].balance);

    }
    if (input.equals("deposit")) {

        System.out.println("How much would you like to deposit?");

        amount = scan.nextInt();
        account_array[id].deposit(amount);
        System.out.println("Balance for account: " + id + " = " + account_array[id].balance);
    }

}
}

1 个答案:

答案 0 :(得分:3)

因为金额&lt; =平衡成立。使用&amp;&amp;而不是||。

if(amount <= balance && amount > 0){