Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在OOP中返回计算值_Java_Oop - Fatal编程技术网

Java 在OOP中返回计算值

Java 在OOP中返回计算值,java,oop,Java,Oop,我不熟悉面向对象编程,所以我不知道如何以字符串形式返回此计算的结果。我正在寻找这样的输出: Person user = new Person(n, a); String status = user.findStatus(); int credit = user.calcCredit(); System.out.println("This person's status is " + status); System.out.println("Their credit is " + credit

我不熟悉面向对象编程,所以我不知道如何以字符串形式返回此计算的结果。我正在寻找这样的输出:

Person user = new Person(n, a);

String status = user.findStatus();
int credit = user.calcCredit();

System.out.println("This person's status is " + status);
System.out.println("Their credit is " + credit);
约翰的信用等级是187

我的问题是,信用似乎没有被计算,或者与对象没有关联。我应该从calcCredit方法返回一些值吗

public class Person
{
  private String name;
  private int age;
  private double rating;

public Person(String name, int age)
{
}

public void setName(String name)
{
    this.name = name;
}

public void setAge(int age)
{
    this.age = age;
}

public void calcCredit()
{
    //credit calculation would go here, for my purpose a static number right now.
    rating = 500;

}

//method to returns the status
public String findStatus()
{
   //return my desired output
}

将此设置为calcCredit()方法:

这样实施:

Person user = new Person(n, a);

String status = user.findStatus();
int credit = user.calcCredit();

System.out.println("This person's status is " + status);
System.out.println("Their credit is " + credit);
将此设置为calcCredit()方法:

这样实施:

Person user = new Person(n, a);

String status = user.findStatus();
int credit = user.calcCredit();

System.out.println("This person's status is " + status);
System.out.println("Their credit is " + credit);
将此设置为calcCredit()方法:

这样实施:

Person user = new Person(n, a);

String status = user.findStatus();
int credit = user.calcCredit();

System.out.println("This person's status is " + status);
System.out.println("Their credit is " + credit);
将此设置为calcCredit()方法:

这样实施:

Person user = new Person(n, a);

String status = user.findStatus();
int credit = user.calcCredit();

System.out.println("This person's status is " + status);
System.out.println("Their credit is " + credit);
您应该有一个getter方法来从类中获取数据。另外,另一种方法是计算相关数据并立即返回。下面是一个基于您的
人员
类的示例:

public class Person {
    //current fields
    public double getCredit() {
        double credit = ...; //calculations here
        //return the value of this variable to clients
        return credit;
    }
}
然后,在客户端代码中,调用此方法并使用以下数据:

public class CreditDemo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the name: ");
        String n = keyboard.nextLine();
        System.out.print("Enter the age: ");
        int a = keyboard.nextInt();

        Person user = new Person(n, a);
        //how do I call findStatus to get the credit associated with the user?
        double credit = user.getCredit();
        System.out.println("Credit for " + user.getName() + " is: " + credit);
    }
}
您应该有一个getter方法来从类中获取数据。另外,另一种方法是计算相关数据并立即返回。下面是一个基于您的
人员
类的示例:

public class Person {
    //current fields
    public double getCredit() {
        double credit = ...; //calculations here
        //return the value of this variable to clients
        return credit;
    }
}
然后,在客户端代码中,调用此方法并使用以下数据:

public class CreditDemo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the name: ");
        String n = keyboard.nextLine();
        System.out.print("Enter the age: ");
        int a = keyboard.nextInt();

        Person user = new Person(n, a);
        //how do I call findStatus to get the credit associated with the user?
        double credit = user.getCredit();
        System.out.println("Credit for " + user.getName() + " is: " + credit);
    }
}
您应该有一个getter方法来从类中获取数据。另外,另一种方法是计算相关数据并立即返回。下面是一个基于您的
人员
类的示例:

public class Person {
    //current fields
    public double getCredit() {
        double credit = ...; //calculations here
        //return the value of this variable to clients
        return credit;
    }
}
然后,在客户端代码中,调用此方法并使用以下数据:

public class CreditDemo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the name: ");
        String n = keyboard.nextLine();
        System.out.print("Enter the age: ");
        int a = keyboard.nextInt();

        Person user = new Person(n, a);
        //how do I call findStatus to get the credit associated with the user?
        double credit = user.getCredit();
        System.out.println("Credit for " + user.getName() + " is: " + credit);
    }
}
您应该有一个getter方法来从类中获取数据。另外,另一种方法是计算相关数据并立即返回。下面是一个基于您的
人员
类的示例:

public class Person {
    //current fields
    public double getCredit() {
        double credit = ...; //calculations here
        //return the value of this variable to clients
        return credit;
    }
}
然后,在客户端代码中,调用此方法并使用以下数据:

public class CreditDemo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter the name: ");
        String n = keyboard.nextLine();
        System.out.print("Enter the age: ");
        int a = keyboard.nextInt();

        Person user = new Person(n, a);
        //how do I call findStatus to get the credit associated with the user?
        double credit = user.getCredit();
        System.out.println("Credit for " + user.getName() + " is: " + credit);
    }
}

您忘记用参数初始化构造函数中的变量

对于您的问题,您可以如下定义findStatus()方法:

public String findStatus() {
    return "The credit rating of " + name + " is " + rating;
}
在main方法中,您可以调用以下方法来打印结果

System.out.println(user.findStatus());

由于您已经为name和age设置了方法,也许还应该为个人的特定属性定义“get”方法,这只是一个想法。

您忘记了用参数初始化构造函数中的变量

对于您的问题,您可以如下定义findStatus()方法:

public String findStatus() {
    return "The credit rating of " + name + " is " + rating;
}
在main方法中,您可以调用以下方法来打印结果

System.out.println(user.findStatus());

由于您已经为name和age设置了方法,也许还应该为个人的特定属性定义“get”方法,这只是一个想法。

您忘记了用参数初始化构造函数中的变量

对于您的问题,您可以如下定义findStatus()方法:

public String findStatus() {
    return "The credit rating of " + name + " is " + rating;
}
在main方法中,您可以调用以下方法来打印结果

System.out.println(user.findStatus());

由于您已经为name和age设置了方法,也许还应该为个人的特定属性定义“get”方法,这只是一个想法。

您忘记了用参数初始化构造函数中的变量

对于您的问题,您可以如下定义findStatus()方法:

public String findStatus() {
    return "The credit rating of " + name + " is " + rating;
}
在main方法中,您可以调用以下方法来打印结果

System.out.println(user.findStatus());

既然您已经为姓名和年龄设置了方法,也许还应该为一个人的特定属性定义“get”方法,这只是一个想法。

现在您已经创建了一个人,接下来您应该计算他的信用评级。要执行此操作,请致电

user.calcCredit();
然后你需要打印出他的状态。您可以通过以下方式执行此操作:

System.out.println("The credit rating of John is " + user.getStatus());
要获得学分,请在类Person中创建公共方法:

public int getCredit() {
    return this.credit;
}

然后您可以在main方法中调用user.getCredit()来打印值。

现在您已经创建了一个人,接下来您应该计算他的信用评级。要执行此操作,请致电

user.calcCredit();
然后你需要打印出他的状态。您可以通过以下方式执行此操作:

System.out.println("The credit rating of John is " + user.getStatus());
要获得学分,请在类Person中创建公共方法:

public int getCredit() {
    return this.credit;
}

然后您可以在main方法中调用user.getCredit()来打印值。

现在您已经创建了一个人,接下来您应该计算他的信用评级。要执行此操作,请致电

user.calcCredit();
然后你需要打印出他的状态。您可以通过以下方式执行此操作:

System.out.println("The credit rating of John is " + user.getStatus());
要获得学分,请在类Person中创建公共方法:

public int getCredit() {
    return this.credit;
}

然后您可以在main方法中调用user.getCredit()来打印值。

现在您已经创建了一个人,接下来您应该计算他的信用评级。要执行此操作,请致电

user.calcCredit();
然后你需要打印出他的状态。您可以通过以下方式执行此操作:

System.out.println("The credit rating of John is " + user.getStatus());
要获得学分,请在类Person中创建公共方法:

public int getCredit() {
    return this.credit;
}
然后可以在main方法中调用user.getCredit()来打印值