算术进阶3级

这是算术级数级别 3。输出寻找 2 个下一项。例如,输入为:1, 4, 11, 24。寻找下2项,即45和76。如何解决?

package Function;


import java.util.Scanner;

/**

 *

 * @author Lenovo

 */


public class ArithmeticProgression {


public static void main(String[] args){        

        int many;


        Scanner keyboard=new Scanner(System.in);

        System.out.print("Put many term: ");

        many= keyboard.nextInt();

        int term[]= new int [many];


        int n= 0;


        for(int z=0; z<many; z++){

            n= n+1;

            System.out.format("%d term"+ " is: ", n);

            term[z] = keyboard.nextInt();

        }


        System.out.print("enter the next many terms: ");

        int range= keyboard.nextInt();


        int term2[] = new int[range+many];


        for(int i = 0; i < many; i++){

            term2[i] = term[i];

        }


        int b3= term2[many-1]-term2[many-2];

        int b2= term2[many-2]-term2[many-3];

        int b1= term2[many-3]-term2[many-4];


        int c2= b3-b2;

        int c1= b2-b1;


        int d= c2-c1;


        for(int q=0; q<range; q++){


            b3= term2[many-1]-term2[many-2];

            b2= term2[many-2]-term2[many-3];

            b1= term2[many-3]-term2[many-4];


            c2= b3-b2;

            c1= b2-b1;


            d= c2-c1;


            int result= term2[many-1]+b3+c2+d;


            System.out.println(result);

            many++;     


        }

    }

}


烙印99
浏览 106回答 1
1回答

慕标琳琳

在 q 循环中,您忘记在每一步更新 term2 数组:&nbsp; &nbsp; for(int q=0; q<range; q++){&nbsp; &nbsp; &nbsp; &nbsp; b3= term2[many-1]-term2[many-2];&nbsp; &nbsp; &nbsp; &nbsp; b2= term2[many-2]-term2[many-3];&nbsp; &nbsp; &nbsp; &nbsp; b1= term2[many-3]-term2[many-4];&nbsp; &nbsp; &nbsp; &nbsp; c2= b3-b2;&nbsp; &nbsp; &nbsp; &nbsp; c1= b2-b1;&nbsp; &nbsp; &nbsp; &nbsp; d= c2-c1;&nbsp; &nbsp; &nbsp; &nbsp; int result= term2[many-1]+b3+c2+d;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(result);&nbsp; &nbsp; &nbsp; &nbsp; term2[many] = result;&nbsp; // you forgot this update&nbsp; &nbsp; &nbsp; &nbsp; many++;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; }这会生成列表 1, 4, 11, 24, 45, 76, 119, 176, 249, 340, 451, ...
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java