I want my program to add every six consecutive weights and display their sum and then calculate the sum of the next six weights and so on, and finally display the total as the addition of all the sums. Additionally if the value of the entered weight is more than 65 i want the counter to increase by one and consider as two weights rather than one (might be a bit confusing, i have added the code and solution)
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();
}
}
this works just fine but the problem is when the number of weights go above 20 and i no longer get the sum after six times but after five times.
the run after the user enters number of weights as 24 i get the answer as this
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
where as what i want is
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
Aucun commentaire:
Enregistrer un commentaire