将值从一种非静态方法传递到另一种方法?

时间:2019-01-20 00:50:07

标签: java methods static non-static

我在使用非静态方法时遇到了麻烦。我试图从一种方法中获取“ side”变量的值,然后将其插入另一个将计算面积的方法中。有没有一种方法可以将方法更改为静态方法?以前在这里回答的问题都没有帮助,我的教科书也没有帮助。

import java.util.*;
public class CubeVolume
{
    int side1;
    int side2;
    int side3;

public void getSides()
{
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the length of side1");
    side1 = input.nextInt();
    System.out.println("Enter the length of side2");
    side2 = input.nextInt();
    System.out.println("Enter the length of side3");
    side3 = input.nextInt();
}

public int getVolume(int side1, int side2, int side3)
{
    int volume = side1 * side2 * side3;
    return volume;
}
public static void main(String[] args)
{
    CubeVolume cube = new CubeVolume();
    cube.getSides();
    cube.getVolume(side1, side2, side3);
 }
}

我认为问题出在我的方法调用cube.getVolume(side1,side2,side3);因为编译器告诉我不能从静态上下文引用非静态变量。

3 个答案:

答案 0 :(得分:1)

无需将任何参数传递给getVolume(),只需使用类变量即可:

import java.util.Scanner;

class CubeVolume {
    private int side1;
    private int side2;
    private int side3;

    private void getSides() {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter the length of side1: ");
        side1 = input.nextInt();
        System.out.print("Enter the length of side2: ");
        side2 = input.nextInt();
        System.out.print("Enter the length of side3: ");
        side3 = input.nextInt();
        input.close();
    }

    private int getVolume() {
        return side1 * side2 * side3;
    }

    private void printAppTitle() {
        System.out.println("Cube Volume Calculator");
        System.out.println("======================");
    }

    public static void main(String[] args) {
        CubeVolume cube = new CubeVolume();
        cube.printAppTitle();
        cube.getSides();
        String cubeVolumeString = String.valueOf(cube.getVolume());
        System.out.println("The cubes volume is: " + cubeVolumeString);
    }
}

用法示例:

Cube Volume Calculator
======================
Enter the length of side1: 3
Enter the length of side2: 4
Enter the length of side3: 5
The cube's volume is: 60

将边长存储在double数组sides中,并处理getSides()中可能的无效输入的替代方法:

import java.util.Scanner;

class CubeVolume {
  private double[] sides;

  CubeVolume() {
    sides = new double[3];
  }

  private void getSides() {
    Scanner scanner = new Scanner(System.in);
    int currentSide = 0;
    while (currentSide < sides.length) {
      System.out.printf("Enter the length of side %d: ", currentSide + 1);
      double nextSide = 0.0;
      input:
      while (scanner.hasNext()) {
        if (scanner.hasNextDouble()){
          nextSide = scanner.nextDouble();
          if (nextSide > 0) {
            sides[currentSide] = nextSide;
            break input;
          } else {
            System.out.println("ERROR: Input number was too small.");
            System.out.printf("Enter the length of side %d: ", currentSide + 1);
          }
        } else {
          System.out.println("ERROR: Invalid input, please input a number.");
          System.out.printf("Enter the length of side %d: ", currentSide + 1);
          scanner.next();
        }
      }
      currentSide++;
    }
    scanner.close();
  }

  private double getVolume() {
    return sides[0] * sides[1] * sides[2];
  }

  private void printAppTitle() {
    System.out.println("Cube Volume Calculator");
    System.out.println("======================");
  }

  public static void main(String[] args) {
    CubeVolume cube = new CubeVolume();
    cube.printAppTitle();
    cube.getSides();
    String cubeVolumeString = String.format("%.2f", cube.getVolume());
    System.out.println("The cube's volume is: " + cubeVolumeString);
  }
}

用法示例2:

Cube Volume Calculator
======================
Enter the length of side 1: a
ERROR: Invalid input, please input a number.
Enter the length of side 1: -1.1
ERROR: Input number was too small.
Enter the length of side 1: 3.4
Enter the length of side 2: 4.7
Enter the length of side 3: 5.8
The cube's volume is: 92.68

答案 1 :(得分:1)

如果要在 main()中使用方法,则这些方法必须是 static ,因为 main()是静态的。因此,您的方法应声明为:

public static void getSides() { .... }
public static int getVolume(int side1, int side2, int side3) { .... }

但是,如果您告诉类的实例从main()方法中的非静态方法开始,则可以避免所有这些情况:

public static void main(String[] args) {
    new CubeVolume().startApp(args);
}

private void startApp(String[] args) {
    CubeVolume cube = new CubeVolume();
    cube.getSides();
    cube.getVolume(side1, side2, side3);
}

现在,您不必在类中的其他方法是静态的,因为您不是从 static main()调用它们的。

答案 2 :(得分:0)

您不需要传递sides参数来获取valume函数,因为您的sides变量将可用于获取valume函数。