我对java很陌生。我正在编写这个程序来计算复利和单利以及我将来会添加的一些其他内容。
语法-
类数据
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 类中创建了一个名为 var 的方法,它需要原理和其他数据来进行计算。因此,如果用户要求计算 CI/SI,它会导入 var 方法并提出问题。现在我无法找出使用这些变量进行计算的方法。 我希望你们能帮助我,欢迎批评和建议,但如果你们能帮助我解决当前的问题,那就更有帮助了,而且我对java真的很陌生
最佳答案
在类 Interest 中创建私有(private)变量 p、r 和 t。创建一个构造函数并传递 3 个值。因此,如果调用方法 si() 或 ci(),则可以简单地使用 this.p、this.r 和 this.t 来计算。您还可以删除 si() 和 ci() 方法中的参数。
关于java - 如何在Java中的另一个类中使用一个类中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61588683/