如何编写Java代码来声明三个Cylinder对象的数组,并提示用户获取数组中每个Cylinder的半径和高度

时间:2013-09-23 00:58:18

标签: java arrays

只是学习和数组的概念是模糊的。我知道如何提示输入;

System.out.println("\nEnter a value for the radius: ");

我可以编写三个数组的代码;

Cylinder[] cylinders = new Cylinder[3]; //creates the individual cylinders
cylinders[0] = new Cylinder(10.0, 5.0); 
cylinders[1] = new Cylinder(11.0, 6.0); 
cylinders[2] = new Cylinder(5.0, 2.0); 

但在提示用户输入并存储在数组中时,不知道如何为数组编码。谁能告诉我这部分的代码应该如何?

4 个答案:

答案 0 :(得分:1)

像这样:

Cylinder[] cylinders = new Cylinder[3];

// Loop using a counter variable, commonly called i,
// as many times as you have array elements:
for (int i = 0; i < cylinders.length; i++) {
    // Each time around the loop, ask for the properties of the cylinder
    System.out.println("\nEnter a value for the radius: ");
    double radius = new Scanner(System.in).nextDouble();
    System.out.println("\nEnter a value for the height: ");
    double height = new Scanner(System.in).nextDouble();

    // Set each array element i to the newly created object
    cylinders[i] = new Cylinder(radius, height);
}

答案 1 :(得分:1)

从如何初始化单个圆柱开始

    //First you need a place to hold the value that the user inputs
    float radius, height;

    //Then you need an object that can read input from the user.
    Scanner sc = new Scanner(System.in);

    //Tell the user what you are expecting them to do
    System.out.println("Enter radius: ");
    //Now use the object to read in a value (You will need to convert it)
    radius = sc.nextFloat();

    //Tell the user what you are expecting them to do
    System.out.println("Enter height: ");
    //Now use the object to read in a value (You will need to convert it)
    height = sc.nextFloat();

    Cylinder cylinders = new Cylinder(radius, height); //creates the individual cylinders

现在如何初始化一个柱面数组。数组与for循环齐头并进。所以我们采用上面的代码并将其包装在for循环中。

    Cylinder[] cylinders = new Cylinder[3];

    //Then you need an object that can read input from the user.
    // No reason to create it multiple times so create it outside the loop.
    Scanner sc = new Scanner(System.in);

    for (int i = 0; i < cylinders.length; i++) {
        //First you need a place to hold the value that the user inputs
        float radius, height;



        //Tell the user what you are expecting them to do
        System.out.println("Enter radius for cylinder[" + i + "]: ");
        //Now use the object to read in a value (You will need to convert it)
        radius = sc.nextFloat();

        //Tell the user what you are expecting them to do
        System.out.println("Enter height for cylinder[" + i + "]: ");
        //Now use the object to read in a value (You will need to convert it)
        height = sc.nextFloat();

        cylinders[i] = new Cylinder(radius, height); //creates the individual cylinders
    }

答案 2 :(得分:0)

您可以接受'String [] args'参数数组形式的用户输入到'main'方法。

使用args [0],args [1]等实例化柱面数组,例如 new Cylinder(10.0,args [0]);

假设您的java类的名称是Cylinders,它将以这种方式调用 - 气缸5.0 6.0 2.0

也许你所拥有的与此相似 - Getting User input with Scanner

那也行。

答案 3 :(得分:0)

要求用户一次输入3个参数并按空格分割。 一旦获得3个参数,就可以调用构造函数和新对象。

使用标准系统输入法或扫描仪都可以。祝你好运