1

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

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

4 回答 4

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
    }
于 2013-09-23T01:28:13.643 回答
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);
}
于 2013-09-23T01:13:49.780 回答
0

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

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

于 2013-09-23T01:19:49.023 回答
0

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

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

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

也许您所拥有的与此类似-使用扫描仪获取用户输入

这也将起作用。

于 2013-09-23T01:09:15.500 回答