Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/67.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 数组中对象的访问属性_Java_Arrays_Object - Fatal编程技术网

Java 数组中对象的访问属性

Java 数组中对象的访问属性,java,arrays,object,Java,Arrays,Object,我目前正在努力完成这段代码。我正在复习明天的考试,所以任何帮助都是好的 关于如何操作的说明在代码的注释中 我不确定我在这段代码中所做的任何事情是否真实有效。我目前的主要问题是如何计算这10个人的总数。你们能帮忙吗 package Fall15Test3; import java.util.*; public class Person { String name; int age; public Person(String name, int age) {

我目前正在努力完成这段代码。我正在复习明天的考试,所以任何帮助都是好的

关于如何操作的说明在代码的注释中

我不确定我在这段代码中所做的任何事情是否真实有效。我目前的主要问题是如何计算这10个人的总数。你们能帮忙吗

package Fall15Test3;
import java.util.*;

public class Person {
    String name;
    int age;



    public Person(String name, int age) {

    }

    public void setName(String name) {

    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {

    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //Create the array of 10 Person objects.
        Person[] personArray = new Person[10];

        //Initialize the array of persons using user's inputs.
        for (int i = 0; i < personArray.length; i++) {
            System.out.print("Please input name: ");
            String name = input.nextLine();
            System.out.print("Please input age: ");
            int age = input.nextInt();
            personArray[i] = new Person(name, age);
        }

        //Calculate the sum of the ages of the 10 people.
        int ageTotal = 0;
        for (int i = 0; i < personArray.length; i++) {
            ageTotal += WHAT DO I PUT HERE;
        }
        System.out.println(ageTotal);
    }
}
包Fall15Test3;
导入java.util.*;
公共阶层人士{
字符串名;
智力年龄;
公众人物(字符串名称,整数年龄){
}
公共void集合名(字符串名){
}
公共字符串getName(){
返回名称;
}
公共无效设置(整数){
}
公共整数getAge(){
回归年龄;
}
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//创建10人对象的数组。
Person[]personArray=新人[10];
//使用用户输入初始化人员数组。
for(int i=0;i
在数组上循环时,可以在循环中的每个索引处访问数组中的对象,并且可以调用对象类型的方法,而无需获取对该对象的附加引用。因此,您可以执行以下操作:

ageTotal += personArray[i].getAge();
这种方法只是直接获取对象,因为
personArray
Person
对象的数组

您还可以执行以下操作,这将显式获取对数组中指定索引处的对象的引用:

for (int i = 0; i < personArray.length; ++i) {
  Person p = personArray[i]; // Make clear we are obtaining the object
  ageTotal += p.getAge();  // get the age set in the Person "p" object
}
您还可以使用流方法获得总额:

int ageTotal = Arrays.stream(personArray).mapToInt(p -> p.age).sum();
System.out.println(ageTotal);
根据您的示例的细节,您可以简单地在输入循环中累积ageTotal。然而,我假设拥有一个单独的循环对于其他处理来说是一个很好的学习示例


此外,Person的示例类在许多方法(例如构造函数)中没有设置名称、年龄。

在数组上循环时,数组中的对象可以在循环中的每个索引处访问,并且可以调用对象类型的方法,而无需获得对对象的附加引用。因此,您可以执行以下操作:

ageTotal += personArray[i].getAge();
这种方法只是直接获取对象,因为
personArray
Person
对象的数组

您还可以执行以下操作,这将显式获取对数组中指定索引处的对象的引用:

for (int i = 0; i < personArray.length; ++i) {
  Person p = personArray[i]; // Make clear we are obtaining the object
  ageTotal += p.getAge();  // get the age set in the Person "p" object
}
您还可以使用流方法获得总额:

int ageTotal = Arrays.stream(personArray).mapToInt(p -> p.age).sum();
System.out.println(ageTotal);
根据您的示例的细节,您可以简单地在输入循环中累积ageTotal。然而,我假设拥有一个单独的循环对于其他处理来说是一个很好的学习示例


另外,您的Person示例类在许多方法(例如,构造函数)中没有设置名称、年龄。

好的,下面是您项目的简明代码;然而,复制我的代码不会教你什么。你需要一行一行地学习,并教会自己这一切的作用,这样你才能学到一些东西。现在这不是我编程的方式(我会把这个人作为一个整体从主方法中分离出来),但我不确定你的老师是否同意,所以这里是你刚刚发布的稍加修改的内容

package Fall15Test3;
import java.util.*;

public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Person[] personArray = new Person[10];

        for (int i = 0; i < personArray.length; i++) {
            System.out.print("Please input name: ");
            String inputName = input.nextLine();
            System.out.print("Please input age: ");
            int inputAge = input.nextInt();
            personArray[i] = new Person(inputName, inputAge);
        }

        int ageTotal = 0;
        for (int i = 0; i < personArray.length; i++) {
            ageTotal += personArray[i].getAge();
        }
        System.out.println(ageTotal);
    }
}
包Fall15Test3;
导入java.util.*;
公共阶层人士{
字符串名;
智力年龄;
公众人物(字符串名称,整数年龄){
this.name=名称;
这个。年龄=年龄;
}
公共字符串getName(){
返回名称;
}
公共整数getAge(){
回归年龄;
}
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
Person[]personArray=新人[10];
for(int i=0;iageTotal+=personArray[i].getAge();
}
系统输出打印项次(总年龄);
}
}

好的,这里是您项目的简明代码;然而,复制我的代码不会教你什么。你需要一行一行地学习,并教会自己这一切的作用,这样你才能学到一些东西。现在这不是我编程的方式(我会把这个人作为一个整体从主方法中分离出来),但我不确定你的老师是否同意,所以这里是你刚刚发布的稍加修改的内容

package Fall15Test3;
import java.util.*;

public class Person {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        Person[] personArray = new Person[10];

        for (int i = 0; i < personArray.length; i++) {
            System.out.print("Please input name: ");
            String inputName = input.nextLine();
            System.out.print("Please input age: ");
            int inputAge = input.nextInt();
            personArray[i] = new Person(inputName, inputAge);
        }

        int ageTotal = 0;
        for (int i = 0; i < personArray.length; i++) {
            ageTotal += personArray[i].getAge();
        }
        System.out.println(ageTotal);
    }
}
包Fall15Test3;
导入java.util.*;
公共阶层人士{
字符串名;
智力年龄;
公众人物(字符串名称,整数年龄){
this.name=名称;
这个。年龄=年龄;
}
公共字符串getName(){
返回名称;
}
公共整数getAge(){
回归年龄;
}
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
Person[]personArray=新人[10];
for(int i=0;i