Results 1 to 1 of 1
-
Using Scanner to compute an average of the values
Java Code:import java.util.*; public class AvgNums { public static void main(String args[]) { Scanner conin = new Scanner(System.in); int count = 0; double sum = 0.0; System.out.println("Enter numbers to average."); // Read and sum numbers. while(conin.hasNext()) { if(conin.hasNextDouble()) { sum += conin.nextDouble(); count++; } else { String str = conin.next(); if(str.equals("done")) break; else { System.out.println("Data format error."); return; } } } System.out.println("Average is " + sum / count); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06-13-2008, 11:09 AM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
sum,product,average program
By chitwood in forum New To JavaReplies: 7Last Post: 01-24-2008, 05:18 AM -
working out wrong average
By gemtez in forum New To JavaReplies: 4Last Post: 12-12-2007, 08:35 AM -
Calculate average age for women and men?
By Legoland in forum New To JavaReplies: 3Last Post: 04-18-2007, 10:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks