每六个连续数字相加以给出它们的总和

时间:2018-02-17 14:53:26

标签: java arrays algorithm for-loop if-statement

我希望我的程序每六个连续加权并显示它们的总和,然后计算下六个权重的总和,依此类推,最后显示总数作为所有总和的加法。 此外,如果输入的权重值大于65,我希望计数器增加1并考虑为两个权重而不是一个(可能有点令人困惑,我添加了代码和解决方案)

package trial;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class trail1 
{
public void display() throws IOException
{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter number of weights");
    int n= Integer.parseInt(br.readLine());
    int Number[] = new int[n];
    int i, sum=0,total=0;
    String m;
    System.out.println("enter the numbers");
    for(i=0;i<n;i++)
    {
        m=br.readLine();
        Number[i]=Integer.parseInt(m);

        if(Number[i]>65)
        {
            sum = sum + Number[i];
            if((i!=0)&&((i%10)==0))
            {
                System.out.println("the sum is out " + sum);
                total=total +sum;
                sum =0;
            }
            else  if(i==(n-2))
            {
                System.out.println("the sum is bad " + sum);
                total = total+sum;
            }
            i++;// this increases the counter by one
        }
        else if(Number[i]<65)
        {
            sum = sum+ Number[i];
            if(((i+1)%6)==0)
            {
                System.out.println("the sum is " + sum);
                total=total +sum;
                sum =0;
            }
            else  if(i==(n-1))
            {
                System.out.println("the sum is bad " + sum);
                total = total+sum;
            }

        }

    }
    System.out.println("the total of the sums is " + total);
}





public static void main(String[] args) throws IOException
{
    trail1 one = new trail1();
    one.display();
}

}

这个工作得很好,但问题是当重量的数量超过20并且我在六次之后但五次之后不再得到总和。

用户输入权重数量后的运行为24我得到了答案

enter number of number
24
enter the numbers
100
100
100
100
100
100
the sum is out 600
100
100
100
100
100
the sum is out 500
100
the sum is bad 100
the total of the sums is 1200

我想要的是

enter number of number
24
enter the numbers
100
100
100
100
100
100
the sum is out 600
100
100
100
100
100
100
the sum is bad 100
the total of the sums is 1200

2 个答案:

答案 0 :(得分:0)

循环从i = 0开始,当i = 5时,它是第六个输入。如所述,当输入> 65时,它被认为是2个权重。所以在i = 5时,它是第六个输入,i * 5是10的倍数。

当在第11个输入i = 10并且if((i!=0)&&((i%10)==0))为真时,这就是它进入条件并在第11个输入之后打印的原因。

使用另一个计数器和i来跟踪计数。这应该对你有所帮助,非常直截了当。

答案 1 :(得分:0)

解决了!取第三个变量'j',并将其用作 -

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ans {
    public void display() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("enter number of weights");
        int n = Integer.parseInt(br.readLine()); // store the number of weights
        int Number[] = new int[n];
        int i, sum = 0, total = 0, j = 0;
        String m;
        System.out.println("enter the numbers");
        for (i = 0; i < n; i++) {
            m = br.readLine();
            Number[i] = Integer.parseInt(m); // store the inputed number in the array 

            if (Number[i] > 65) { // if the number > 65
                sum += Number[i];
                if ((i != 0) && ((j % 5) == 0) && (j != 0) && (i != (n - 2))) {
                    System.out.println("the sum is out " + sum);
                    total = total + sum;
                    sum = 0;
                    j = -1;
                } else if (i == (n - 2)) {
                    System.out.println("the sum is bad " + (sum * 1 / 6));
                    total += sum;
                }
                i++;
                ++j;
            } else if (Number[i] < 65) {
                sum = sum + Number[i];
                if (((i + 1) % 6) == 0) {
                    System.out.println("the sum is " + sum);
                    total = total + sum;
                    sum = 0;
                } else if (i == (n - 1)) {
                    System.out.println("the sum is bad " + sum);
                    total = total + sum;
                }

            }

        }
        System.out.println("the total of the sums is " + total);
    }

    public static void main(String[] args) throws IOException {
        Ans one = new Ans();
        one.display();
    }

}

输出正确。在上面的代码中,我使用了(sum * 1/6)你看,我没有将sum的值重置为5然后加100,但输出仍然是相同的,因为100 = 1/6 th 600,这将是所有可能输入所需的输出。这是输出 -

enter number of weights
24
enter the numbers
100
100
100
100
100
100
the sum is out 600

100
100
100
100
100
100
the sum is bad 100
the total of the sums is 1200