+ 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
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);
}
}
+ 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);
}
}
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());
}
}
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);
}
}
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);
}
}
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());
}
}
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);
}
}
- 1
Um, the code you posted is empty, where's the attempt in your code?
- 1
you should see it now
- 1
Dino Wun would you see it now ?
- 1
Yes
- 1
thanks guys for the help!!
- 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 ^^