jeudi 22 septembre 2022

Finding sum in a random array list

I'm trying to make a random array list in range [-250, 250] and print the numbers, then the sum just once, at the end of the code for all of the numbers below. here is my code:

public class Main {
  public static void main(String[] args) {
      Random rand = new Random();
      int max = 250;
      int min = -250;
      for  ( int  i = 1 ; i <= 50 ; i ++){
          int rez = min + rand.nextInt(max) * 2;
          System.out.println("The number is : " + i);
          int sum = rez + rez;
          System.out.println("The sum is: " + rez);
      } 

This is the code so far. It works. But I want to print the sum just once, at the end of the code for all of the numbers above. My code so far prints the sum individually.

I also tried it like this


    for  ( int  i = 1 ; i <= 50 ; i ++) {
            int rez = min + rand.nextInt(max) * 2;
            System.out.println("The number is : " + i);
            int sum = rez + rez;
        }
            System.out.println("The sum is: " + rez); 

but it gives me this error "Cannot resolve symbol 'rez'" Can someone please tell me what I did wrong?




Aucun commentaire:

Enregistrer un commentaire