简体   繁体   English

如何在 Java 中的另一个 class 中使用来自 class 的变量

[英]How can I use variables from a class in another class in Java

I'm very new to java.我对 java 很陌生。 I am writing this program that calculates Compound and Simple Interest and some other things which I'll add in the future.我正在编写这个程序来计算复利和单利以及其他一些我将在未来添加的东西。

Syntax-句法-

Class Data Class 数据

import java.util.Scanner;
public class DATA {
    public static void main(String[] args) {
        Scanner m=new Scanner(System.in);
        System.out.println("What do you want to calculate SI/CI");
        String choice=m.nextLine();
        {
            if (choice.equalsIgnoreCase("si") || choice.equalsIgnoreCase("ci"))
                Interest.var();
            if (choice.equalsIgnoreCase("si")){
                Interest.ci();
            }
        }


    }
}

Class Interest Class 兴趣

import java.util.Scanner;

public class Interest {
    public static void var(){
        Scanner m=new Scanner(System.in);
        System.out.println("Enter the principle");
        double p=m.nextDouble();
        System.out.println("Enter the rate");
        double r=m.nextDouble();
        System.out.println("Enter the time");
        double t=m.nextDouble();
    }
     public static void si(double p, double r, double t){
        double si=(p*r*t)/100;
        System.out.println("The Simple Interest is "+si);
    }
    public static void ci(double p, double r, double t){
        double ci=p*Math.pow((1+(r/100)),(t))-p;
        System.out.println("The Compound Interest is "+ci);
    }
}

I've created a method called var in the Interest class which asks for the principle and other data for calculation.我在 Interest class 中创建了一个名为 var 的方法,它要求计算原理和其他数据。 So If the user asks to calculate CI/SI, it imports the var method and asks the questions.因此,如果用户要求计算 CI/SI,它会导入 var 方法并提出问题。 now I can't figure out a way to use those variable for calculation.现在我想不出一种方法来使用这些变量进行计算。 I hope you guys can help me, criticisms and suggestions are welcomed but if you can help me with the current problem, that would be more then helpful and again I'm really new to java我希望你们能帮助我,欢迎批评和建议,但如果你能帮助我解决当前的问题,那将更有帮助,我对 java 真的很陌生

Create private variables p,r and t inside the class Interest.在 class Interest 中创建私有变量 p,r 和 t。 Create a constructor and pass the 3 values.创建一个构造函数并传递 3 个值。 So if you call the method si() or ci(), you can simply use this.p, this.r and this.t to calculate.所以如果调用方法si()或者ci(),可以简单的使用this.p、this.r和this.t来计算。 You can also remove the params in si() and ci() methods.您还可以删除 si() 和 ci() 方法中的参数。

When you define a class in JAVA, usually if you have variables that you use in more than one function, you define them like global variables for that class and use getters and setters methods to get or set them, like: When you define a class in JAVA, usually if you have variables that you use in more than one function, you define them like global variables for that class and use getters and setters methods to get or set them, like:

import java.util.Scanner;

public class Interest {

    private double p;
    private double r;
    private double t;
    ......
    public void setP(double p){this.p=p;}
    public void setR(double r){this.r=r;}
    public void setT(double t){this.t=t;}
    public double getP(){return p;}
    public double getR(){return r;}
    public double getT(){return t;}
}

However, your code could looks like:但是,您的代码可能如下所示:

import java.util.Scanner;

public class Interest {
    private double p;
    private double r;
    private double t;
    public static void var(){
        Scanner m=new Scanner(System.in);
        System.out.println("Enter the principle");
        p=m.nextDouble();
        System.out.println("Enter the rate");
        r=m.nextDouble();
        System.out.println("Enter the time");
        t=m.nextDouble();
    }
     public static void si(){
        double si=(p*r*t)/100;
        System.out.println("The Simple Interest is "+si);
    }
    public static void ci(){
        double ci=p*Math.pow((1+(r/100)),(t))-p;
        System.out.println("The Compound Interest is "+ci);
    }
}

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

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