简体   繁体   English

Java方法| 传递值

[英]Java methods | Passing values

I am a beginner in Java and my professor wants us to create a program that involves some math. 我是Java的初学者,我的教授希望我们创建一个涉及数学的程序。 The thing is that he wants the calculations in a separate method. 问题是他希望用单独的方法进行计算。 Can someone please help me. 有人可以帮帮我吗。 The program will not run the result for some reason. 该程序由于某种原因将无法运行结果。 I tried looking around this website and could not find the answer. 我尝试浏览此网站,但找不到答案。 Here is the code below: 这是下面的代码:

import javax.swing.JOptionPane;

public class forFun {
    public static void main(String[] args)
    {
        double x, y, z;
        String xVal, yVal, zVal;
        xVal = JOptionPane.showInputDialog("Enter first integer: ");
        yVal = JOptionPane.showInputDialog("Enter second integer: ");
        zVal = JOptionPane.showInputDialog("Enter third integer: ");

        x = Double.parseDouble(xVal);
        y = Double.parseDouble(yVal);
        z = Double.parseDouble(zVal);

        System.exit(0);
    }

    public static void sumOfStocks(double x, double y, double z)
    {
        double result = x * y * z;

        System.out.println("The product of the integers is: " + result);
        System.exit(0);
    }
}

Here is the example of how the code should be 这是代码应如何的示例

import javax.swing.JOptionPane;

public class forFun {
public static void main(String[] args)
{
    double x, y, z;
    String xVal, yVal, zVal;
    xVal = JOptionPane.showInputDialog("Enter first integer: ");
    yVal = JOptionPane.showInputDialog("Enter second integer: ");
    zVal = JOptionPane.showInputDialog("Enter third integer: ");

    x = Double.parseDouble(xVal);
    y = Double.parseDouble(yVal);
    z = Double.parseDouble(zVal);
    double result = sumOfStocks(x, y, z);
    System.out.println("The result is %d", result);
    System.exit(0);
}

public static double sumOfStocks(double x, double y, double z)
{
    double result = x * y * z;
    return result;

}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM