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

标签 java class object

所以这是我的完整代码,我已经完成了重命名之类的操作,但它仍然没有检测到我的对象“atm”。 我总是收到此错误。

C:\Users\USER\Documents\JCreator LE\MyProjects\oreo\src\oreo.java:30: error: cannot find symbol System.out.println("\nYour Savings balance is now: " + 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;
    }    
}

最佳答案

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

atm.gbalance

atm.gbalance()

关于java - 在 Java 中未将类对象检测为类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26395862/

相关文章:

java - 使用Json访问特定属性

java - 如何实现javafx标签值变化监听器

java - 如何在 Scala 的单元测试中利用不同类的变量?

Javascript 是否可以创建特定对象的原型(prototype)数组?

java - WebApp 或 Tomcat 中的 JDBC 驱动程序

javascript - 冗余和继承?

PHP 类作用域与 call_user_func() 混淆

javascript - 赋值的同时赋值

javascript - Object.keys 执行时间

java - 类设计最小化内存使用 : if an object instance variable (non-primitive) is null does it still use the necessary reference bytes?