Jump to content

array


AJBM
 Share

Recommended Posts

Boas!

lista={12, 5, -21, 10, -345, 22, 50, -125, 80,-1} eu tenho este array e tenho que somar os numeros positivos eu ja fiz isto

int [] lista={12, 5, -21, 10, -345, 22, 50, -125, 80,-1};
      int soma=0;
  
      for(int i=0;lista[i]>0;i++){
          soma+= lista[i];
         
      } 
    System.out.println(soma);

so que so me da a soma dos 2 primeiros 12+5 , eu acho que quando ele encontra um numero menor que 0 ele para, mas eu queria que ele continuasse, alguem me pode ajudar?

Link to comment
Share on other sites

ja consegui resolver o problema e fica aqui a resoluçao

int [] lista={12, 5, -21, 10, -345, 22, 50, -125, 80,-1};
      int soma=1;
     
  
      for(int i=0;i<lista.length;i++){
          if(lista[i]>0){
              
          
          soma+= lista[i];
         
          }
          
      } 
    System.out.println("a soma e de : "+soma);
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.