在Java中未将类对象检测为类

时间:2014-10-16 03:44:18

标签: java class object

所以这里是我的完整代码,我已经完成了重命名这样的事情,它仍然没有检测到我的对象" atm"。 我总是收到这个错误。

  

C:\ Users \ USER \ Documents \ JCreator LE \ MyProjects \ oreo \ src \ oreo.java:30:   错误:找不到符号                         System.out.println(" \ n您的储蓄余额现在是:" + atm.gbalance +" \ n");

import java.util.Scanner;

public class oreo {
    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        Account atm = new Account();

        atm.intBalance(0.00);

        boolean atm_status = true;
        while(atm_status) {
            System.out.print("1.Deposit\n");
            System.out.print("\n2.Withdraw");
            System.out.print("\n3.Check Balance");
            System.out.print("\n4.Exit");
            System.out.print("\nOption: ");
            int option = input.nextInt();

            switch(option) {
                case 1:
                    System.out.print("How much would you like to deposit?");
                    double deposit = input.nextDouble();
                    atm.deposit(deposit);
                    break;
                case 2:
                    double withdraw;
                    System.out.print("Your balance is:" + atm.gbalance + "\n");
                    System.out.print("How much would you like to withdraw?" );
                    withdraw= input.nextDouble();
                    System.out.println("\nYour Savings balance is now: " + atm.gbalance + "\n");
                    break;
                case 3:
                    System.out.println("Your current balance is: " + atm.gbalance + "\n");
                    break;
                case 4:
                    atm_status = false;
                    break;
            }
        }
    }
}


class Account {
    double balance;

    void intBalance(double set) {
        balance = set;
    }

    void deposit(double depo) {
        balance +=depo;
    }

    void withdraw(double with){
        balance -=with;
    }

    double gbalance(){
        return balance;
    }    
}

3 个答案:

答案 0 :(得分:4)

atm.gbalance是一个方法,而不是Account类中的变量,因此在访问它时需要括号()。所以改变这些

atm.gbalance

atm.gbalance()

答案 1 :(得分:0)

在调用类方法()时,您忘记了括号atm.gbalance;

System.out.println("\nYour Savings balance is now: " + atm.gbalance() + "\n");

System.out.println("Your current balance is now: " + atm.gbalance() + "\n");

答案 2 :(得分:0)

System.out.println("\nYour Savings balance is now: " + atm.gbalance + "\n"

好吧,要么你想使用变量余额还是方法gbalance() atm.gbalance错误或者是拼写错误(排除“g”),或者错误地将()添加到结尾以将其定义为方法。