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

huangapple 未分类评论51阅读模式
英文:

How can I use variables from a class in another class in Java

问题

以下是您要翻译的内容:

我对Java非常陌生。我正在编写一个计算复利和简单利息以及将来要添加的其他内容的程序。

语法:

数据类

import java.util.Scanner;
public class DATA {
    public static void main(String[] args) {
        Scanner m=new Scanner(System.in);
        System.out.println("您想计算简单利息(SI)还是复利(CI)?");
        String choice=m.nextLine();
        {
            if (choice.equalsIgnoreCase("si") || choice.equalsIgnoreCase("ci"))
                Interest.var();
            if (choice.equalsIgnoreCase("si")){
                Interest.ci();
            }
        }


    }
}

利息类

import java.util.Scanner;

public class Interest {
    public static void var(){
        Scanner m=new Scanner(System.in);
        System.out.println("请输入本金");
        double p=m.nextDouble();
        System.out.println("请输入利率");
        double r=m.nextDouble();
        System.out.println("请输入时间");
        double t=m.nextDouble();
    }
     public static void si(double p, double r, double t){
        double si=(p*r*t)/100;
        System.out.println("简单利息为:"+si);
    }
    public static void ci(double p, double r, double t){
        double ci=p*Math.pow((1+(r/100)),(t))-p;
        System.out.println("复利为:"+ci);
    }
}

我在利息类中创建了一个名为var的方法,该方法询问计算所需的本金和其他数据。因此,如果用户要求计算CI/SI,它将导入var方法并提出问题。现在我无法找到一种方法来使用这些变量进行计算。
希望您能帮助我,欢迎批评和建议,但如果您能帮助我解决目前的问题,那将非常有帮助,再次申明我真的很新到Java。

英文:

I'm very new to 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

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

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. So If the user asks to calculate CI/SI, it imports the var method and asks the questions. 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

答案1

得分: 0

在类Interest内部创建私有变量p、r和t。创建一个构造函数,并传递这3个值。这样,如果你调用si()或ci()方法,你可以简单地使用this.p、this.r和this.t来进行计算。你也可以在si()和ci()方法中删除参数。

英文:

Create private variables p,r and t inside the class Interest. Create a constructor and pass the 3 values. So if you call the method si() or ci(), you can simply use this.p, this.r and this.t to calculate. You can also remove the params in si() and ci() methods.

答案2

得分: 0

当您在JAVA中定义一个类时,通常如果您在多个函数中使用的变量,您会将它们定义为该类的全局变量,并使用getter和setter方法来获取或设置它们,例如:

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);
    }
}
英文:

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

huangapple
  • 本文由 发表于 2020年5月4日 17:08:36
  • 转载请务必保留本文链接:https://java.coder-hub.com/61588683.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定