Im trying to average the user input by a string of numbers, It prints out without a problem but the output is very wrong
Enter some numbers:
0,4,3,5
48.0
import java.util.Scanner;
public class avg {
public static void main(String[] args) {
System.out.println("Enter some numbers");
Scanner in=new Scanner(System.in);
String nums=in.nextLine();
double ress = 0;
double add=0;
for (int i = 0; i < nums.length(); i++)
{
char x = nums.charAt(i);
add=add+x;
if (x == ',') continue;
}
ress=(double)(add/nums.length());
System.out.println(ress);
}
}
custurd12 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.