【问题标题】:Method Calling in a Java ClassJava 类中的方法调用
【发布时间】:2016-10-05 18:46:01
【问题描述】:

所以我很确定我只是在这里犯了一个基本错误,但是我无法在我的任何一个静态方法中获取返回值来到达 main 方法。我对该区域的输出每次都为零。出了什么问题?

非常感谢您的任何意见/见解!

公共课项目_4 { public static void main(String[] args){

    Scanner input = new Scanner(System.in);
    int choice = 0;
    double celsius, fahrenheit=0, inches=0, centimeters=0, length=0, width=0, height=0, boxVolume=0, radius=0, sphereVolume=0, result=0;
    double boxArea = box(result,  length,  width,  height);
    double sphereArea = sphere(result, radius);
    char doAgain = 'y';

    while (doAgain == 'y' || doAgain == 'Y')
    {

    System.out.print(" Main Menu \n 1. Celsius to Fahrenheit \n 2. Inches to Centimeters \n "
            + "3. Find Volume and Area of a box \n 4. Find Volume and Area of a sphere \n"
            + "Please select one of the four choices \nThen press enter to continue");
    choice = input.nextInt();


    if (choice == 1)
    {
        System.out.println("This program will convert a celsius value into a fahrenheit one. \n");

        System.out.println("Please enter the tempature for conversion: ");
        celsius = input.nextInt();

        fahrenheit = 1.8 * celsius + 32.0;

        System.out.println(celsius + " degree(s) in celcius" + " is " + fahrenheit + 
                " in fahrenheit. \n");

        System.out.println("Do you want to continue?\n: " +
                   "enter \'Y\' for another round\n " +
                  " enter \'N\'   to end the program\n");

        doAgain = input.next().charAt(0);
    }

    else if (choice == 2)
    {
        System.out.println("This program will convert inches to centimeters. \n");


        System.out.println("Please enter the length for conversion: ");
        inches = input.nextInt();


        centimeters = 2.54 * inches;


        System.out.println(inches + " inches is " + centimeters + " centimeters. \n");

        System.out.println("Do you want to continue?\n: " +
                   "enter \'Y\' for another round\n " +
                  " enter \'N\'   to end the program\n");

        doAgain = input.next().charAt(0);
    }

    else if (choice == 3)
    {
        System.out.println("Enter length: ");
        length = input.nextDouble();

        System.out.println("Enter height: ");
        height = input.nextDouble();

        System.out.println("Enter width: ");
        width = input.nextDouble();

        boxVolume = length * width * height;

        System.out.println("The area of your box is: " + boxArea + "\n" 
                + "The volume of your box is: " + boxVolume);

        System.out.println("Do you want to continue?\n: " +
                   "enter \'Y\' for another round\n " +
                  " enter \'N\'   to end the program\n");
        doAgain = input.next().charAt(0);

    }

    else if (choice == 4)
    {
        System.out.println("Enter radius: ");
        radius = input.nextDouble();

        sphereVolume = 4.0/3.0 * 3.14159 *Math.pow(radius, 3);

        System.out.println("The area of your sphere is: " + sphereArea + "\n" 
                + "The volume of your sphereVolume is: " + sphereVolume);

        System.out.println("Do you want to continue?\n: " +
                   "enter \'Y\' for another round\n " +
                  " enter \'N\'   to end the program\n");
        doAgain = input.next().charAt(0);
    }

    else
    {
        System.out.println("That is not a valid option. Please restart and try again");

        }
    }
}

public static double box(double result, double length, double width, double height)
{
    result = length * width;
    return result;
}

public static double sphere(double result, double radius)
{
    result = 3.14 * (radius * radius);
    return 0;
}

}

【问题讨论】:

  • 嗯,当所有参数的值为 0 时,您正在调用 box 和 sphere。这些方法不会在每次使用时使用“当前”变量值再次调用boxArea 和 sphereArea...
  • 我是这么认为的,但一位同学说它会从 main 方法中获取这些值。我可以在我的方法声明/签名中将值设置为scannerObject.nextDouble 还是必须重新定义静态方法中的值?

标签: java methods call return-value


【解决方案1】:

变量result, length, width, height的值被初始化为0。

所以当您调用box(result, length, width, height) 时,这类似于像这样调用box(0, 0, 0, 0)。此外,您不需要发送result 变量,您可以在方法本身中创建一个。并且不要发送高度变量,因为您没有在方法内部使用它。

Inside box 方法将 0 与 0 相乘,然后返回返回的 0。 Sphere 方法也是如此,其中一件事是将 return 0 更改为 return result。

示例代码

double length = 5.0;
double width = 5.0;
double boxArea = box(length, width);
System.out.println("Box Area is " + boxArea);

public static double box (double length, double width){
  double result = length * width;
  return result
}

输出:Box Area is 25.0

更新:-

如果您想要length and width 的用户输入,那么您可以执行以下操作

示例代码

double length;
double width;
double boxArea;

Scanner input = new Scanner(System.in);

System.out.println("Enter the value for length and width")

length = input.nextDouble();
width = input.nextDouble();

boxArea = box(length, width);

// You can call box method inside main method with user input values of length and width.

【讨论】:

  • 但是如何在其他方法中使用来自 main 方法的用户输入呢?我需要来自主要方法的用户输入在我的区域方法中。它们不能由程序员定义,因为那会使程序无关紧要。感谢您对如何在方法中将变量设置为零的非常好的解释。
  • 原来是这样,我也是,注册时间肯定要花点时间。
猜你喜欢
  • 1970-01-01
  • 2020-07-31
  • 2011-02-11
  • 1970-01-01
  • 2019-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多