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

我在使用非静态方法时遇到问题。我试图从一个方法中获取我的“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); 因为编译器告诉我不能从静态上下文中引用非静态变量。


森栏
浏览 77回答 3
3回答

Cats萌萌

如果你想在main()中使用方法,那么这些方法必须是静态的,因为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);}现在类中的其他方法不需要是静态的,因为您不是从静态 main()调用它们。

青春有我

无需传入任何参数getVolume(),只需使用类变量即可:import java.util.Scanner;class CubeVolume {&nbsp; &nbsp; private int side1;&nbsp; &nbsp; private int side2;&nbsp; &nbsp; private int side3;&nbsp; &nbsp; private void getSides() {&nbsp; &nbsp; &nbsp; &nbsp; Scanner input = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter the length of side1: ");&nbsp; &nbsp; &nbsp; &nbsp; side1 = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter the length of side2: ");&nbsp; &nbsp; &nbsp; &nbsp; side2 = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Enter the length of side3: ");&nbsp; &nbsp; &nbsp; &nbsp; side3 = input.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; input.close();&nbsp; &nbsp; }&nbsp; &nbsp; private int getVolume() {&nbsp; &nbsp; &nbsp; &nbsp; return side1 * side2 * side3;&nbsp; &nbsp; }&nbsp; &nbsp; private void printAppTitle() {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Cube Volume Calculator");&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("======================");&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; CubeVolume cube = new CubeVolume();&nbsp; &nbsp; &nbsp; &nbsp; cube.printAppTitle();&nbsp; &nbsp; &nbsp; &nbsp; cube.getSides();&nbsp; &nbsp; &nbsp; &nbsp; String cubeVolumeString = String.valueOf(cube.getVolume());&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("The cubes volume is: " + cubeVolumeString);&nbsp; &nbsp; }}示例用法:Cube Volume Calculator======================Enter the length of side1: 3Enter the length of side2: 4Enter the length of side3: 5The cube's volume is: 60将边长存储在双精度数组中的替代方法sides,并处理 中可能的无效输入getSides():import java.util.Scanner;class CubeVolume {&nbsp; private double[] sides;&nbsp; CubeVolume() {&nbsp; &nbsp; sides = new double[3];&nbsp; }&nbsp; private void getSides() {&nbsp; &nbsp; Scanner scanner = new Scanner(System.in);&nbsp; &nbsp; int currentSide = 0;&nbsp; &nbsp; while (currentSide < sides.length) {&nbsp; &nbsp; &nbsp; System.out.printf("Enter the length of side %d: ", currentSide + 1);&nbsp; &nbsp; &nbsp; double nextSide = 0.0;&nbsp; &nbsp; &nbsp; input:&nbsp; &nbsp; &nbsp; while (scanner.hasNext()) {&nbsp; &nbsp; &nbsp; &nbsp; if (scanner.hasNextDouble()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nextSide = scanner.nextDouble();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (nextSide > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sides[currentSide] = nextSide;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break input;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("ERROR: Input number was too small.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Enter the length of side %d: ", currentSide + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("ERROR: Invalid input, please input a number.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("Enter the length of side %d: ", currentSide + 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanner.next();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; currentSide++;&nbsp; &nbsp; }&nbsp; &nbsp; scanner.close();&nbsp; }&nbsp; private double getVolume() {&nbsp; &nbsp; return sides[0] * sides[1] * sides[2];&nbsp; }&nbsp; private void printAppTitle() {&nbsp; &nbsp; System.out.println("Cube Volume Calculator");&nbsp; &nbsp; System.out.println("======================");&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; CubeVolume cube = new CubeVolume();&nbsp; &nbsp; cube.printAppTitle();&nbsp; &nbsp; cube.getSides();&nbsp; &nbsp; String cubeVolumeString = String.format("%.2f", cube.getVolume());&nbsp; &nbsp; System.out.println("The cube's volume is: " + cubeVolumeString);&nbsp; }}示例用法 2:Cube Volume Calculator======================Enter the length of side 1: aERROR: Invalid input, please input a number.Enter the length of side 1: -1.1ERROR: Input number was too small.Enter the length of side 1: 3.4Enter the length of side 2: 4.7Enter the length of side 3: 5.8The cube's volume is: 92.68

交互式爱情

您不需要传递边参数来获取值函数,因为您的边变量将可用于获取值函数。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java