【问题标题】:I am trying to create an array of objects from my class "Cerc",which has a property of type int and one of type string [closed]我正在尝试从我的类“Cerc”创建一个对象数组,该类具有 int 类型的属性和 string 类型的属性[关闭]
【发布时间】:2019-12-22 23:24:59
【问题描述】:
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {

        Scanner scan=new Scanner(System.in);

       //raza of type integer
       //nume of type String

    System.out.print("Dimension is = " );
    int n=scan.nextInt();
    Cerc c[]=new Cerc[n]; //array of objects



    //init array
        for (int i = 0; i <c.length ; i++) {
            c[i]=new Cerc();

            System.out.print("Raza=");
            c[i].raza=scan.nextInt();
            System.out.print("Nume=");
            c[i].nume=scan.nextLine();

        //Printing array
        for (int i = 0; i <n ; i++) {
            c[i].afisare(); 
        }
    }*main*
}*class*

谁能解释我如何使用键盘的用户输入来初始化具有不同类型参数的对象数组。

【问题讨论】:

标签: java arrays object


【解决方案1】:

您需要创建适当的构造函数和/或 setter 和 getter。下面是一个例子:

import java.util.Scanner;

class Person {
    String name;
    int age;
    double salary;

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

    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }
}

public class Test {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = 0;
        Person persons[] = null;
        System.out.print("Enter dimension: ");
        try {
            n = Integer.parseInt(scan.nextLine());
            persons = new Person[n]; // array of objects

            // init array
            for (int i = 0; i < persons.length; i++) {
                System.out.print("Enter name: ");
                String name = scan.nextLine();
                System.out.print("Enter age: ");
                int age = Integer.parseInt(scan.nextLine());
                System.out.print("Enter salary: ");
                double salary = Double.parseDouble(scan.nextLine());
                persons[i] = new Person(name, age, salary);
            }
        } catch (Exception e) {
            System.out.println("Invalid input");
        }
        // Printing array
        for (int i = 0; i < n; i++) {
            System.out.println(persons[i]);
        }
    }
}

示例运行:

Enter dimension: 2
Enter name: John
Enter age: 23
Enter salary: 1234
Enter name: Sam
Enter age: 34
Enter salary: 4321
Person [name=John, age=23, salary=1234.0]
Person [name=Sam, age=34, salary=4321.0]

【讨论】:

    【解决方案2】:

    read...() 将在第一个空格字符或\n 处停止。在只有一个 int(扫描仪已经读取)的行上调用 readInt() 后,调用 readLine() 将返回行上剩余的内容(在这种情况下只有空格)。

    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            Scanner scan=new Scanner(System.in);
    
            //raza of type integer
            //nume of type String
            System.out.print("Dimension is = " );
            // The replaceAll("[^\\d]+", "") removes all characters that aren't digits
            int n=Integer.parseInt(scan.nextLine().replaceAll("[^\\d]+", ""));
            // Arrays of objects are declared like this:
            Cerc[] c = new Cerc[n]; //array of objects
    
            //init array
            for (int i = 0; i <c.length ; i++) {
                c[i]=new Cerc();
    
                System.out.print("Raza=");
                // The replaceAll("[^\\d]+", "") removes all characters that aren't digits
                c[i].raza = Integer.parseInt(scan.nextLine().replaceAll("[^\\d]+", ""));
                System.out.print("Nume=");
                c[i].nume = scan.nextLine();
            }
            //Printing array
            for (int i = 0; i <n ; i++) {
                c[i].afisare(); 
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-08
      • 1970-01-01
      • 2020-04-24
      相关资源
      最近更新 更多