简体   繁体   English

如何将变量值从主 class 传递给另一个 class?

[英]How to pass the value of variables from main class to another class?

Hello everyone I'm currently creating a simple program.大家好我目前正在创建一个简单的程序。 I have 2 classes the first one is calculator and the second one is parameters_return.我有 2 个类,第一个是计算器,第二个是 parameters_return。 What I want to happen is that when I read the values of x and y in my second class i want to use them in my first class Unfortunately, I can't run the program because it has an error.我想要发生的是,当我在我的第二个 class 中读取 x 和 y 的值时,我想在我的第一个 class 中使用它们不幸的是,我无法运行该程序,因为它有一个错误。 The code is below please help me with regards to this matter.下面的代码请帮助我解决这个问题。 I'm just self studying I really want to learn Java.我只是自学 我很想学 Java。

Code in (first class) calculator class is: (一流)计算器 class 中的代码是:

class calculator {
    //with parameters with return type
    int add(int a, int b) {
        return (a + b);
    }

    //with parameters without return type
    void sub(int a, int b) {
        System.out.print(a - b);
    }

    //without parameters with return type
    int mul() {
        parameters_return s1 = new parameters_return();
        int c = (s1.x) * (s1.y);
        return c;
    }

    //without parameters without return type
    void div() {
        parameters_return s2 = new parameters_return();
        int c = (s2.x) / (s2.y);
        System.out.println("Division = " + c);
    }
}

Code in my (second class) parameters_return class is:我的(第二类)parameters_return class 中的代码是:

class parameters_return {
    int x, y;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        y = sc.nextInt();
        calculator perform = new calculator();

        //addition
        int z = perform.add(x, y);
        System.out.println("Added value = " + z);

        //subtraction
        System.out.println("Subtracted value = ");
        perform.sub(x, y);

        //multiplication
        z = perform.mul();
        System.out.println("Multiplication value = ");

        //division
        perform.div();
    }
}

Is there any way to get values from main class and can be used in another class?有没有办法从主 class 获取值并可以在另一个 class 中使用?

import java.util.*;
class ParametersReturn {
    static int x, y;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        y = sc.nextInt();
        Calculator perform = new Calculator();

        //addition
        int z = perform.add(x, y);
        System.out.println("Added value = " + z);

        //subtraction
        System.out.println("Subtracted value = ");
        perform.sub(x, y);

        //multiplication
        z = perform.mul();
        System.out.println("Multiplication value = " + z);

        //division
        perform.div();
    }
}

This should be your ParametersReturn Class and make sure you should start your class name with capital letter, you are using Scanner class to use it you have to import java.util package. And to use these variables in Calculator class make these variables static这应该是您的参数归还Class,并确保您应该启动带有大写字母的class名称,您使用的是Scanner class来使用它来使用它

class Calculator {
    //with parameters with return type
    int add(int a, int b) {
        return (a + b);
    }

    //with parameters without return type
    void sub(int a, int b) {
        System.out.print(a - b);
    }

    //without parameters with return type
    int mul() {
        ParametersReturn s1 = new ParametersReturn();
        int c = (s1.x) * (s1.y);
        return c;
    }

    //without parameters without return type
    void div() {
        ParametersReturn s2 = new ParametersReturn();
        int c = (s2.x) / (s2.y);
        System.out.println("Division = " + c);
    }
}

And in multiplication you forgot to print the value of z在乘法中你忘了打印 z 的值

Hello everyone I'm currently creating a simple program.大家好,我目前正在创建一个简单的程序。 I have 2 classes the first one is calculator and the second one is parameters_return.我有 2 个类,第一个是计算器,第二个是 parameters_return。 What I want to happen is that when I read the values of x and y in my second class i want to use them in my first class Unfortunately, I can't run the program because it has an error.我想要发生的是,当我在我的第二个 class 中读取 x 和 y 的值时,我想在我的第一个 class 中使用它们不幸的是,我无法运行该程序,因为它有错误。 The code is below please help me with regards to this matter.下面的代码请帮助我解决这个问题。 I'm just self studying I really want to learn Java.我只是自学我真的很想学习Java。

Code in (first class) calculator class is: (一级)计算器 class 中的代码是:

class calculator {
    //with parameters with return type
    int add(int a, int b) {
        return (a + b);
    }

    //with parameters without return type
    void sub(int a, int b) {
        System.out.print(a - b);
    }

    //without parameters with return type
    int mul() {
        parameters_return s1 = new parameters_return();
        int c = (s1.x) * (s1.y);
        return c;
    }

    //without parameters without return type
    void div() {
        parameters_return s2 = new parameters_return();
        int c = (s2.x) / (s2.y);
        System.out.println("Division = " + c);
    }
}

Code in my (second class) parameters_return class is:我的(二等)parameters_return class 中的代码是:

class parameters_return {
    int x, y;
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        x = sc.nextInt();
        y = sc.nextInt();
        calculator perform = new calculator();

        //addition
        int z = perform.add(x, y);
        System.out.println("Added value = " + z);

        //subtraction
        System.out.println("Subtracted value = ");
        perform.sub(x, y);

        //multiplication
        z = perform.mul();
        System.out.println("Multiplication value = ");

        //division
        perform.div();
    }
}

Is there any way to get values from main class and can be used in another class?有什么方法可以从主 class 获取值,并且可以在另一个 class 中使用?

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

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