繁体   English   中英

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

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

我对 java 很陌生。 我正在编写这个程序来计算复利和单利以及其他一些我将在未来添加的东西。

句法-

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 兴趣

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);
    }
}

我在 Interest class 中创建了一个名为 var 的方法,它要求计算原理和其他数据。 因此,如果用户要求计算 CI/SI,它会导入 var 方法并提出问题。 现在我想不出一种方法来使用这些变量进行计算。 我希望你们能帮助我,欢迎批评和建议,但如果你能帮助我解决当前的问题,那将更有帮助,我对 java 真的很陌生

在 class Interest 中创建私有变量 p,r 和 t。 创建一个构造函数并传递 3 个值。 所以如果调用方法si()或者ci(),可以简单的使用this.p、this.r和this.t来计算。 您还可以删除 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:

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;}
}

但是,您的代码可能如下所示:

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