为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

幕布斯7119047 2023-07-13 16:50:25
我是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 个数字,但由于某种原因,它打印的总数比应有的要高得多。输入示例:1234-1输出:84当正确答案应该是 10 时,它打印出 84。
查看完整描述

3 回答

?
慕妹3242003

TA贡献1824条经验 获得超6个赞

您的第一个 for 循环是将数组中的每个索引分配给最新的输入。您不需要嵌套 for 循环来完成此作业。这是一个带有注释的示例,解释了代码的不同方面。


Scanner in = new Scanner(System.in);

int[] numbersList = new int[10];

int amount = 0;

int total = 0;


do { //stop at negative

    int number = in.nextInt();

    if (number < 0) {

        // if a negative number is entered then stop looping

        break;

    }


    amount += 1;


    // use modulo arithmetic to assign numbers, the accessed index will

    // range from 0 to 9

    numbersList[amount % 10] = number;


    // iterate over numbersList and sum entries

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

        total += numbersList[i];

    }


    // output total

    System.out.println(total);


    // reset total to 0

    total = 0;

} while(number >= 0); // this is redundant since we check earlier


System.out.println(total);


查看完整回答
反对 回复 2023-07-13
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

  1. 只需声明numberint number;,它就会自动取值 0。无需询问 Scanner。第一个 int 永远不会被使用,因为你在 while 循环中请求另一个 int。

  2. 在第 n 轮中,您将用当前轮数替换前 n+1 个整数(0 <= i <= n -> n+1 轮;改为使用i < amount)。如果您“玩”超过 9 轮,这将导致异常。

  3. 每次替换单个整数后,您都会遍历整个数组。请注意,该总数永远不会重置。

  4. 您并不是在输入负数后立即停止,而是也用这个负数运行一轮然后终止。break;因此,如果您发现结果是否定的,请使用 a 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


查看完整回答
反对 回复 2023-07-13
?
猛跑小猪

TA贡献1858条经验 获得超8个赞

下面的代码也有效。


        ArrayList<Integer> numbersList = new ArrayList<>();

        Scanner in = new Scanner(System.in);

        int number = in.nextInt();

        int total = 0;

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

        //store all numbers in ArrayList

        numbersList.add(number);

        //for the first 10 numbers we can just add them as follows

        if(numbersList.size()<=10) {

                total += number;

                System.out.println(total);

        }

        //if user imputs more than 10 numbers we need to count the total of the last 10 numbers so:

        else

        {

                //Restore total to 0 for recount

                total =0;

                //iterate Arraylist backwards for the last 10 values inputed in it

        for(int j = 10; j >=1  ; j--) {

                total += numbersList.get(numbersList.size()-j);

        }

                System.out.println(total);

        }

        //get next number

        in = new Scanner(System.in);

        number = in.nextInt();

        }


        System.out.println(total);


查看完整回答
反对 回复 2023-07-13
  • 3 回答
  • 0 关注
  • 114 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信