0

这是有关操作的说明: 显示所有三个帐户的名称和余额。使用 if 语句,打印金额最大的银行账户对象的名称和余额(根据您选择的值)。您可能需要向 Bank 类添加其他方法(获取方法)。

这是我的代码:

import java.util.*;
public class Bank
{
    public Bank()
    {
        double checkingBal = 0.0;
        double savingsBal = 0.0;
    }
    public static void main(String[] args)
    {
        System.out.println("Problem 1");
        Bank tom = new Bank();
        Bank rohan = new Bank();
        Bank parth = new Bank();
        tom.setChecking(10000);
        parth.setChecking(60000);
        rohan.setChecking(700000);
        larmo();  
    }
    public void setChecking(double val)
    {
        double checkingBal = val;
    }
    public Bank larmo()
    {
        System.out.println(tom.getChecking());
        System.out.println(rohan.getChecking());
        System.out.println(parth.getChecking());
        if (tom.getChecking()>parth.getChecking() && tom.getChecking()>rohan.getChecking())
        {
            System.out.println("Name: Tom, Balance: "+tom.getChecking());
        }
        if (parth.getChecking()>tom.getChecking() && parth.getChecking()>rohan.getChecking())
        {
            System.out.println("Name: Parth, Balance: "+parth.getChecking());
        }
        if (rohan.getChecking()>tom.getChecking() && rohan.getChecking()>parth.getChecking())
        {
            System.out.println("Name: Rohan, Balance: "+rohan.getChecking());
        }
        System.out.println("Congratulations to the richest man in the bank");
    }
    public double getChecking()
    {
        return checkingBal;
    }
    }

我收到此错误: 无法从静态上下文引用非静态方法 larmo()

为什么,我能做些什么来解决这个问题。

4

2 回答 2

1

在您的情况下,您应该将方法更改为public static void larmo()

static因为该方法不需要访问 Bank 实例。这也允许从静态上下文中调用它。

void因为你从方法中没有return任何价值。

于 2021-03-21T06:38:55.340 回答
0

我们不能直接从静态成员访问非静态成员

如果您想访问该larmo method()

  • 您应该将 larmo 方法设为静态
  • 或者,创建 Bank 类的实例并使用该实例调用larmo 方法
于 2021-03-21T07:13:30.670 回答