mercredi 10 février 2021

Why am I not able to print the value outside th e for loop

I am trying to print the value of max but not able to print. But once I put the Print statement inside the for loop then I can print it. Can anyone please help me out

class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
    // your code goes here
    try{
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[]  = new int[n];
        for(int i = 0; i< n; i++){
            a[i]  = sc.nextInt();
            
        }
       int max = a[0];
       
        for(int i =0; i< n;i++){
           int sum  =a[i];
            while(a[i+1] > a[i] && i+1<n){
               
                sum = sum+a[i+1];
                i++;
            }
                if(sum > max){
                    max = sum ;
                    
                }
            
             
        }
       System.out.println(max);
        
        
        
    }
    catch(Exception e){
            return;
        }

    
}

}

Aucun commentaire:

Enregistrer un commentaire