Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本Java:向函数传递数据_Java - Fatal编程技术网

基本Java:向函数传递数据

基本Java:向函数传递数据,java,Java,我对Java非常陌生,正在尝试使用在线资源自学,因此我为这个新问题道歉,并提前感谢您的帮助 我的目标是:创建一个类。给变量赋值。创建一个函数。创建一个对象。调用函数以显示变量 问题:“错误:找不到符号test1.printResult(结果);” 我的猜测是,我没有将“result”传递给printResult();正确。在使用运算符的类中没有结果变量。必须先创建此变量,然后才能将其传递给MathDemoprintResult(…)方法。例如: package usingoperators;

我对Java非常陌生,正在尝试使用在线资源自学,因此我为这个新问题道歉,并提前感谢您的帮助

我的目标是:创建一个类。给变量赋值。创建一个函数。创建一个对象。调用函数以显示变量

问题:“错误:找不到符号test1.printResult(结果);”


我的猜测是,我没有将“result”传递给printResult();正确。

在使用运算符的
类中没有
结果
变量。必须先创建此变量,然后才能将其传递给
MathDemo
printResult(…)
方法。例如:

package usingoperators;

class MathDemo 
{
    // create integer variable and initiate it to 3
    int result = 1 + 2; //result is now 3

    // create printResult function with result parameter
    void printResult(int result) // result is passed to printResult function
    {
        System.out.println(result); // prints result
    }
}    
public class UsingOperators 
{
   public static void main(String[] args) 
    {      
        MathDemo test1 = new MathDemo(); // creates test1 object
        test1.printResult(result); // calls the printResult function to print result
    }
}

UsingOperators
类中没有
result
变量。必须先创建此变量,然后才能将其传递给
MathDemo
printResult(…)
方法。例如:

package usingoperators;

class MathDemo 
{
    // create integer variable and initiate it to 3
    int result = 1 + 2; //result is now 3

    // create printResult function with result parameter
    void printResult(int result) // result is passed to printResult function
    {
        System.out.println(result); // prints result
    }
}    
public class UsingOperators 
{
   public static void main(String[] args) 
    {      
        MathDemo test1 = new MathDemo(); // creates test1 object
        test1.printResult(result); // calls the printResult function to print result
    }
}

主方法中没有变量
result
,因此Java不知道传递给
printResult()
的是什么。您应该在main方法中本地定义一个
result
变量,这样就可以将它传递给该方法(而不是在OOP中称之为method,而不是function)

我还实现了一个
add()
方法,因此您可以传递两个数字,然后返回总和

数学课:

public class UsingOperators{

    public static void main(String[] args){
        int reuslt = 1;
        MathDemo test1 = new MathDemo(); // creates test1 object
        test1.printResult(result); // calls the printResult function to print result
    }
}
类来使用MathDemo:

package usingoperators;

public class MathDemo 
{

    // Calculate a + b and return result
    public int add(int a, int b) {
        return a + b;
    }

    // create printResult function with result parameter
    public void printResult(int result) // result is passed to printResult function
    {
        System.out.println(result); // prints result
    }
}    

主方法中没有变量
result
,因此Java不知道传递给
printResult()
的是什么。您应该在main方法中本地定义一个
result
变量,这样就可以将它传递给该方法(而不是在OOP中称之为method,而不是function)

我还实现了一个
add()
方法,因此您可以传递两个数字,然后返回总和

数学课:

public class UsingOperators{

    public static void main(String[] args){
        int reuslt = 1;
        MathDemo test1 = new MathDemo(); // creates test1 object
        test1.printResult(result); // calls the printResult function to print result
    }
}
类来使用MathDemo:

package usingoperators;

public class MathDemo 
{

    // Calculate a + b and return result
    public int add(int a, int b) {
        return a + b;
    }

    // create printResult function with result parameter
    public void printResult(int result) // result is passed to printResult function
    {
        System.out.println(result); // prints result
    }
}    

您将结果作为参数提供给函数,但在main方法中结果未知。 试试这个

package usingoperators;

public class UsingOperators 
{
   public static void main(String[] args) 
    {
        MathDemo test1 = new MathDemo(); // creates test1 object

        int result = test1.add(1, 2); // Calculate 1 + 2 and store result in variable
        test1.printResult(result); // pass result to method to print it
    }
}

您将结果作为参数提供给函数,但在main方法中结果未知。 试试这个

package usingoperators;

public class UsingOperators 
{
   public static void main(String[] args) 
    {
        MathDemo test1 = new MathDemo(); // creates test1 object

        int result = test1.add(1, 2); // Calculate 1 + 2 and store result in variable
        test1.printResult(result); // pass result to method to print it
    }
}

printfresult(int result)
中删除参数
int result
,因为您不需要它。然后在main方法中编写
test1.printResult()printResult(int result)
中删除参数
int result
,因为您不需要它。然后在main方法中编写
test1.printResult()