【问题标题】:How to get variables from one static method and use them in another method如何从一个静态方法中获取变量并在另一种方法中使用它们
【发布时间】:2015-09-27 22:48:18
【问题描述】:

我正在尝试使用名为 getBMI 的静态方法中的变量:

public static double getBMI(int weightKG, int heightCM)
{
    Scanner input = new Scanner(System.in);
    // Input Weight
    System.out.print("Enter your weight in kilograms: ");
            weightKG = input.nextInt();
    System.out.println();
    // Input Height
    System.out.print("Enter your height in centimeters: ");
    heightCM = input.nextInt();
    System.out.println();
            return heightCM;
            return weightKG;
}

并在另一个名为 calculateMetricBMI 的静态方法中使用它:

public static void calculateMetricBMI()
{
    getBMI();
    System.out.println("A body mass index of 20-25 is considered \"normal\"");
    double bmiMetric = weightKG/Math.pow(heightCM/100.0, 2);
    System.out.print("Your BMI is " + bmiMetric);
} 

但是,我在尝试 getBMI(); 时遇到错误。在calculateMetricBMI中。

编辑:

决定给getBMI()添加参数; 现在它显示 getBMI(int weightKG, heightCM);

但是我得到了这个错误:

'.class' 预期

';'预计

';'预计

意外的类型 必需:值 找到:类

【问题讨论】:

  • 您遇到的具体错误是什么,请您将其发布在问题上吗?
  • 方法不能两次返回值。此外,您可能应该将方法返回的结果存储在某处。
  • 此链接可能会有所帮助。 stackoverflow.com/questions/457629/…
  • 编写一个方法来读取每个输入并返回单个值。这些方法应该使用 Scanner。然后在计算值时使用结果,可选择将它们传递给离散计算函数。静态变量(此处不存在)没有保证。
  • 如果你想让别人帮你做作业,至少要选择一个答案。

标签: java variables


【解决方案1】:

您收到一个错误,因为您调用 getBMI() 时没有任何参数,它需要 2 个整数才能被调用。

您也不能从 1 个方法返回 2 个变量。

试试这个:

public static void calculateMetricBMI() {
    double weightKG = getWeight();
    double heightCM = getHeight();

    System.out.println("A body mass index of 20-25 is considered \"normal\"");
    double bmiMetric = weightKG/Math.pow(heightCM/100.0, 2);
    System.out.print("Your BMI is " + bmiMetric);
} 

public static double getWeight() {
    Scanner input = new Scanner(System.in);
    // Input Weight
    System.out.println("Enter your weight in kilograms: ");
    double weightKG = input.nextInt();

    return weightKG;
}

public static double getHeight() {
    Scanner input = new Scanner(System.in);
    // Input Height
    System.out.println("Enter your height in centimeters: ");
    double heightCM = input.nextInt();

    return heightCM;
}

在你的主要调用中

    calculateMetricBMI();

这是一个非常多余的解决方案,您始终可以只在 calculateMetricBMI() 中请求输入,而不必调用其他 2 个方法。

【讨论】:

  • 还要注意getBMI() 应该采用零输入,而不是两个。那些应该是局部变量
【解决方案2】:
  1. 您不能在一个方法中同时返回两个值。但是,我将您的代码更改为直接返回您的计算结果。

  2. 如果您要在其中初始化方法,则您的方法不需要参数。只需在您的方法中创建它们即可。

  3. 如果你愿意使用带参数的方法,那么在调用方法的时候,当然要带参数了

getBMI() 在这里不起作用,例如,您应该调用 getBMI(50.0, 165.0),但这是一个不好的例子,因为它无论如何都不会起作用。

看看下面的代码,它更清晰,会得到更好的结果。

public static double getBMIMetric()
{
    Scanner input = new Scanner(System.in);
    // Input Weight
    System.out.print("Enter your weight in kilograms: ");
    int weightKG = input.nextDouble();
    System.out.print("Enter your height in centimeters: ");
    int heightCM = input.nextInt();
    System.out.println();
    return weightKG/Math.pow(heightCM/100.0, 2);
}

public static void calculateMetricBMI()
{
    System.out.println("A body mass index of 20-25 is considered \"normal\"");
    double bmiMetric = getBMIMetric();
    System.out.print("Your BMI is " + bmiMetric);
} 

【讨论】:

    【解决方案3】:

    您需要使用两个参数调用getBMI();

    此外,您不能返回 2 个不同的变量,尽管这可能会导致语法错误。

    另外需要注意的是,您需要保存 getBMI(); 发送给您的内容。

    例如:int weightKG = getBMI()getBMI() 应该只返回一个值,而不是您在代码中描述的两个值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-10
      • 2017-04-13
      • 2019-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多