猿问

打印数组中最后 10 个数字的总和?

我是java新手,我一生都无法弄清楚如何解决这个问题。我假设问题与 for(int i = 0; i <= amount; i++){ 行以及之后的所有内容有关,我想不出我做错了什么。我想知道我的代码是否至少接近工作解决方案?任何帮助,将不胜感激。


问题:编写允许用户重复输入数字的 Java 代码。每次用户输入一个数字时,程序应打印出最后 10 个数字的总数(如果输入的数字为 10 个或更少,则打印出所有数字)。


按照我的方式,如果用户输入负数,程序就会停止。


    Scanner in = new Scanner(System.in);

    int[] numbersList = new int[10];

    int number = in.nextInt();

    int amount = 0;

    int total = 0;

    while(number>=0){ //stop at negative

        number = in.nextInt();

        amount += 1;

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

            numbersList[i] = number;

            for(int j = 0; j < numbersList.length; j++){

                total += numbersList[j];

            }

        }

    }

    System.out.println(total);

我正在努力让程序正确打印数组的最后 10 个数字,但由于某种原因,它打印的总数比应有的要高得多。


输入示例:


1


2


3


4


-1


输出:


84


当正确答案应该是 10 时,它打印出 84。


幕布斯7119047
浏览 146回答 3
3回答

慕妹3242003

您的第一个 for 循环是将数组中的每个索引分配给最新的输入。您不需要嵌套 for 循环来完成此作业。这是一个带有注释的示例,解释了代码的不同方面。Scanner in = new Scanner(System.in);int[] numbersList = new int[10];int amount = 0;int total = 0;do { //stop at negative&nbsp; &nbsp; int number = in.nextInt();&nbsp; &nbsp; if (number < 0) {&nbsp; &nbsp; &nbsp; &nbsp; // if a negative number is entered then stop looping&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; amount += 1;&nbsp; &nbsp; // use modulo arithmetic to assign numbers, the accessed index will&nbsp; &nbsp; // range from 0 to 9&nbsp; &nbsp; numbersList[amount % 10] = number;&nbsp; &nbsp; // iterate over numbersList and sum entries&nbsp; &nbsp; for(int i = 0; i < numbersList.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; total += numbersList[i];&nbsp; &nbsp; }&nbsp; &nbsp; // output total&nbsp; &nbsp; System.out.println(total);&nbsp; &nbsp; // reset total to 0&nbsp; &nbsp; total = 0;} while(number >= 0); // this is redundant since we check earlierSystem.out.println(total);

慕姐4208626

只需声明number为int number;,它就会自动取值 0。无需询问 Scanner。第一个 int 永远不会被使用,因为你在 while 循环中请求另一个 int。在第 n 轮中,您将用当前轮数替换前 n+1 个整数(0 <= i <= n -> n+1 轮;改为使用i < amount)。如果您“玩”超过 9 轮,这将导致异常。每次替换单个整数后,您都会遍历整个数组。请注意,该总数永远不会重置。您并不是在输入负数后立即停止,而是也用这个负数运行一轮然后终止。break;因此,如果您发现结果是否定的,请使用 a&nbsp;number(因此,请在从扫描仪获取结果后进行测试)。因此,您可以对以下数组求总和(省略尾随 0):[2] //2[2,2] //2 + 2 + 2 = 6[3,2] //6 + 3 + 2 = 11[3,3] // 11 + 3 + 3 = 17[3,3,3] // 17 + 3 + 3 + 3 = 26[4,3,3] // 26 + 4 + 3 + 3 = 36[4,4,3] // 36 + 4 + 4 + 3 = 47[4,4,4] // 47 + 4 + 4 + 4 = 59[4,4,4,4] // 59 + 4 + 4 + 4 + 4 = 75[-1,4,4,4] // 75 - 1 + 4 + 4 + 4 = 86[-1,-1,4,4] // 86 - 1 - 1 + 4 + 4 = 92[-1,-1,-1,4] // 92 - 1 - 1 - 1 + 4 = 93[-1,-1,-1,-1] // 91 - 1 - 1 - 1 - 1 = 89[-1,-1,-1,-1,-1] // 89 - 1 - 1 - 1 - 1 - 1 = 84

猛跑小猪

下面的代码也有效。&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Integer> numbersList = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; Scanner in = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; int number = in.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; int total = 0;&nbsp; &nbsp; &nbsp; &nbsp; while (number>=0) { //stop at negative&nbsp; &nbsp; &nbsp; &nbsp; //store all numbers in ArrayList&nbsp; &nbsp; &nbsp; &nbsp; numbersList.add(number);&nbsp; &nbsp; &nbsp; &nbsp; //for the first 10 numbers we can just add them as follows&nbsp; &nbsp; &nbsp; &nbsp; if(numbersList.size()<=10) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += number;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(total);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //if user imputs more than 10 numbers we need to count the total of the last 10 numbers so:&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Restore total to 0 for recount&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total =0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //iterate Arraylist backwards for the last 10 values inputed in it&nbsp; &nbsp; &nbsp; &nbsp; for(int j = 10; j >=1&nbsp; ; j--) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; total += numbersList.get(numbersList.size()-j);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(total);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //get next number&nbsp; &nbsp; &nbsp; &nbsp; in = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; number = in.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(total);
随时随地看视频慕课网APP

相关分类

Java
我要回答