java - 如何使用前一个值来增加数组的元素?

标签 java arrays loops

我无法弄清楚如何根据用户输入来增加元素,同时将其应用于整个数组。

问题:编写一个程序来创建一个 double 组,其中数组的大小由用户提供。

用户还将提供数组的第一个值和增量值。 您的程序必须使用数组第一个元素的第一个值完全填充数组。 数组的下一个元素的值等于前一个元素加上增量。该程序基本上生成一个算术级数。 数组填满后,必须打印以检查正确性。 您的程序必须在数组大小、起始值和增量的任何合理值下运行。

public static void main(String[] args) {

    Scanner kbd = new Scanner(System.in);
    System.out.println("This program fills an array of doubles using an"
            + " initial value, the array size and an increment value.");
    System.out.println("Please enter the desired size of the array: ");

    int size = kbd.nextInt();
    double[] array1 = new double[size];

    System.out.println("Please enter the value of the first element: ");                     
     array1[0] = kbd.nextDouble(); 
     System.out.println("Please enter the increment value: ");

     double inc=kbd.nextDouble();
     double total =array1[0]+inc;

        for (int i =0; i < array1.length;i++)
        {
            System.out.println(total++);
        }
}

到目前为止,我的代码仅添加到第一个元素,但我不确定如何继续定位每个元素,以便输出如下所示:

该程序使用初始值、数组大小和增量值填充 double 组。

Please enter the desired size of the array:6
Please enter the value of the first element: 0
Please enter the increment value:2
array[0]:0.00
array[1]:2.00
array[2]:4.00
array[3]:6.00
array[4]:8.00
array[5]:10.00

最佳答案

下面的代码包含两种可以使用的方法。对于一种方法,您可以从索引 1 开始循环遍历数组,并将 inc 添加到前一个索引值。另一种方法是在每次循环运行时使用“total”变量,将 inc 的值添加到“total”变量的值中。对于任一方法,您都应该构建一些内容来检查用户输入数组长度为零的值的事件的输入。

import java.util.Scanner;

public class HelloWorld{

    public static void main(String[] args) {

        Scanner kbd = new Scanner(System.in);
        System.out.println("This program fills an array of doubles using an"
                + " initial value, the array size and an increment value.");
        System.out.println("Please enter the desired size of the array: ");

        int size = kbd.nextInt();
        double[] array1 = new double[size];

        System.out.println("Please enter the value of the first element: ");                     
        array1[0] = kbd.nextDouble(); 
        System.out.println("Please enter the increment value: ");

        double inc=kbd.nextDouble();
        double total=array1[0];

        /* remove this line to use method 1 --
        System.out.println(array1[0]);
        for (int i =1; i < array1.length;i++){
            array1[i] = (total = total + inc);
            System.out.println(array1[i]);
        } 
        -- remove this line to use method one */

        // Method two is below.
        System.out.println(array1[0]);
        for (int i =1; i < array1.length;i++){
            array1[i] = array1[i - 1] + inc;
            System.out.println(array1[i]);
        }
    }
}

关于java - 如何使用前一个值来增加数组的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58245451/

相关文章:

html - 当我们使用 CanVG 导出到 PNG 时,多行标题不起作用 - Highcharts

java - for inside for 在java中打印 "table"(适合初学者)

c - 如何计算MIPS中两个值之间的偶数之和?

java - 开关出现奇怪的错误

java - 使用同步静态方法时出现 IllegalMonitorStateException : object not locked by thread before wait(),

java - 从 Docx 中删除内容控件

php - MySQL/PHP 数组到 Javascript

javascript - 来自 localStorage 问题的 JSON.parse()

Java SoftReference 奇怪的行为

java - 如何正确关闭 Android 应用程序?