【问题标题】:non-static method annot be referenced from a static context [duplicate]不能从静态上下文中引用非静态方法[重复]
【发布时间】:2023-03-22 19:32:02
【问题描述】:

这是代码的一部分,但是当我执行时出现错误 该程序是关于线性规划的 对不起我的英语,或者愚蠢的问题

“错误:无法从静态上下文中引用非静态方法 get_total_profit(String)

output = get_total_profit(ip1); 在输出下^ 1 个错误”

import java.io.*;
import java.lang.*;
import java.util.*;
import java.text.DecimalFormat;

public class CandidateCode {

 String get_total_profit(String input1) {
    String[] inputs = input1.split(",");
    // Piece of farm land in square kilometer
    float L = Float.valueOf(inputs[0]);
    // Fertilizer in kg
    float F = Float.valueOf(inputs[1]);
    // Insecticide in kg
    float P = Float.valueOf(inputs[2]);
    // Fertilizer required in kg for square kilometer of Wheat
    float F1 = Float.valueOf(inputs[3]);
    // Insecticide required in kg for square kilometer of Wheat
    float P1 = Float.valueOf(inputs[4]);
    // Fertilizer required in kg for square kilometer of Rice
    float F2 = Float.valueOf(inputs[5]);
    // Insecticide required in kg for square kilometer of Rice
    float P2 = Float.valueOf(inputs[6]);
    // Selling price of wheat per square kilometer
    float S1 = Float.valueOf(inputs[7]);
    // Selling price of rice per square kilometer
    float S2 = Float.valueOf(inputs[8]);

    // Result Variables
    float totalRiceInsecUsed = 0f;
    float totalRiceFertUsed = 0f;
    float totalWheatInsecUsed = 0f;
    float totalWheatFertUsed = 0f;
    float areaOfWheat = 0.00f;
    float areaOfRice = 0.00f;
    float amount = 0.00f;

    while (true) {
        if ((L == areaOfRice + areaOfWheat)
                || P == totalRiceInsecUsed + totalWheatInsecUsed
                || F == totalRiceFertUsed + totalWheatFertUsed || F2 == 0
                || F1 == 0 || P2 == 0 || P1 == 0) {
            break;
        }

        float calRiceProfit = Math.min(F / F2, P / P2) * S2;
        float calWheatProfit = Math.min(F / F1, P / P1) * S1;

        if (calRiceProfit > calWheatProfit) {
            float areaInsecUsed = P / P2;
            float areaFertUsed = F / F2;
            if (areaInsecUsed > areaFertUsed) {
                L = L - areaFertUsed;
                F2 = 0;
                totalRiceFertUsed = totalRiceFertUsed + F2;
                areaOfRice = areaOfRice + areaFertUsed;
                amount = amount + areaFertUsed * S2;
            } else if (areaInsecUsed < areaFertUsed) {
                L = L - areaInsecUsed;
                P2 = 0;
                totalRiceInsecUsed = totalRiceInsecUsed + areaInsecUsed;
                areaOfRice = areaOfRice + areaInsecUsed;
                amount = amount + areaInsecUsed * S2;
            }
        } else {
            float areaInsecUsed = P / P1;
            float areaFertUsed = F / F1;
            if (areaInsecUsed > areaFertUsed) {
                L = L - areaFertUsed;
                F1 = 0;
                totalWheatFertUsed = totalWheatFertUsed + F1;
                areaOfWheat = areaOfWheat + areaFertUsed;
                amount = amount + areaFertUsed * S1;
            } else if (areaInsecUsed < areaFertUsed) {
                L = L - areaInsecUsed;
                P1 = 0;
                totalWheatInsecUsed = totalWheatInsecUsed + areaInsecUsed;
                areaOfWheat = areaOfWheat + areaInsecUsed;
                amount = amount + areaInsecUsed * S1;
            }
        }

    }
    DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);
    df.setMinimumFractionDigits(2);
    return df.format(amount) + "," + df.format(areaOfWheat) + ","
            + df.format(areaOfRice);
}
}

【问题讨论】:

    标签: java string


    【解决方案1】:

    如果你不想为你的类创建一个新实例,那么将你的方法定义为静态,如下所示:

    public static String get_total_profit(String input1) {
        /**
        * your code goes here
        */
    }
    

    【讨论】:

    • 非常感谢,这肯定是个愚蠢的错误:D 我的错
    • 哈哈......它发生了。没有问题。
    【解决方案2】:

    如果您必须在静态上下文中引用方法 get_total_profit 并且不能将方法 get_total_profit 设为静态,则可以为包含静态上下文中的 get_total_profit 方法的类声明一个引用变量。用下面的代码替换你的行:

        CandidateCode candidateCode=new CandidateCode();
        output=candidateCode.get_total_profit(ip1);
    

    【讨论】:

    • 替换哪一行?
    • 输出 = get_total_profit(ip1);将其替换为 CandidateCode 类型的局部变量的声明,并使用该变量引用方法 get_total_profit。
    【解决方案3】:

    当您尝试使用静态方法之外的非静态方法时,会发生此错误。 我不知道您的其余代码,但您似乎试图从您的 main-method 之类的东西中调用(非静态)get_total_profit-operation?

    有两种可能性:一方面,您可以将 get_total_profit 更改为静态(您可能不希望这样做) 或者另一方面,您可以在 ip1 类中创建一个函数,而无需引用“this”的参数。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 2016-09-17
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 2016-08-18
      相关资源
      最近更新 更多