+ 1

Arraylist

Anyone could you tell was wrong with this code ? The program you are given declares an even nums ArrayList. Write a program to take numbers as input and add them an ArrayList while the size of the ArrayList isn't equal to 3. Then calculate and output the average of all values in integers. Sample Input 5 2 4 Sample Output 3 https://code.sololearn.com/ca11a18A8a09

21st May 2021, 7:50 PM
JRAMAHES
JRAMAHES - avatar
13 Réponses
+ 8
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> evennums = new ArrayList<Integer>(); int sum = 0; while(evennums.size()<3){ int num = scanner.nextInt(); evennums.add(num); sum += num; } int average = sum/evennums.size(); System.out.println(average); } }
13th Sep 2021, 12:15 PM
Abiel M
Abiel M - avatar
+ 2
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> evennums = new ArrayList<Integer>(); while(evennums.size()<3){ int num = scanner.nextInt(); //your code goes here evennums.add(num); } //calculate and output the average integer value int a = (evennums.get(0)+ evennums.get(1)+evennums.get(2))/3; double result = Math.floor(a); int b = (int) result; System.out.println(b); } }
24th Jan 2022, 12:21 PM
Максим Малов
Максим Малов - avatar
0
import java.util.ArrayList; import java.util.Scanner; //Please Subscribe to My Youtube Channel //Channel Name: Fazal Tuts4U public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> evennums = new ArrayList<Integer>(); int total = 0; while(evennums.size()<3){ int num = scanner.nextInt(); evennums.add(num); total = total + num; } System.out.println(total/evennums.size()); } }
5th Sep 2021, 6:18 AM
Fazal Haroon
Fazal Haroon - avatar
0
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> evennums = new ArrayList<Integer>(); int sum=0; while(evennums.size()<3){ int num = scanner.nextInt(); //your code goes here evennums.add(num); sum+=num; } //calculate and output the average integer value int average =sum/evennums.size(); System.out.println(average); } }
25th Apr 2022, 9:52 PM
Adu, William Nyarko
0
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> evennums = new ArrayList<Integer>(); int sum = 0; while(evennums.size()<3){ int num = scanner.nextInt(); //your code goes here evennums.add(num); sum = sum + num; } //calculate and output the average integer value int average = sum / evennums.size(); System.out.println(average); } }
13th Jul 2022, 8:05 AM
Nesar Ahmed
Nesar Ahmed - avatar
0
import java.util.ArrayList; import java.util.Scanner; public class ArrayListTest { public static void main(String[ ] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> evennums = new ArrayList<Integer>(); int sum = 0; while(evennums.size()<3){ System.out.println("enter numbers with enter everytime"); int num = scanner.nextInt(); evennums.add(num); sum = sum+num; } System.out.println(sum/evennums.size()); } }
30th Jul 2022, 2:06 AM
Ariaie M
Ariaie M - avatar
0
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); ArrayList<Integer> evennums = new ArrayList(); int sum = 0; while(evennums.size()<3){ int num = scanner.nextInt(); evennums.add(num); sum += num; } int average = sum/evennums.size(); System.out.println(average); } }
15th Nov 2022, 7:38 AM
Pooja Patel
Pooja Patel - avatar
- 1
Um, the code you posted is empty, where's the attempt in your code?
21st May 2021, 7:51 PM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
- 1
you should see it now
21st May 2021, 7:52 PM
JRAMAHES
JRAMAHES - avatar
- 1
Dino Wun would you see it now ?
21st May 2021, 7:54 PM
JRAMAHES
JRAMAHES - avatar
- 1
Yes
21st May 2021, 7:56 PM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
- 1
thanks guys for the help!!
21st May 2021, 8:33 PM
JRAMAHES
JRAMAHES - avatar
- 2
corrected code: https://code.sololearn.com/cG88G2x3RokB/?ref=app however, you don't really need the array creation to store user input values, as you work only with 3 values... so you could avoid it and just do a for loop to get 3 numbers from user wich you add together (or add them linearly without loop), then you only have to divide by 3 the sum ^^
21st May 2021, 8:22 PM
visph
visph - avatar