Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-11-2008, 04:04 PM
Zebra's Avatar
Member
 
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
Zebra is on a distinguished road
Averages of user inputed values (Need Help)
Hey. I need some help with this one:

"Write a program that gets n numbers from a user. The program then displays two averages. One average is that of all the even numbers that were entered, and the second is for the odds."

RULES:
-No more than one loop
-No more than one if-else statement.
-Output must say The average of inputed values is... (Example: The average of 23, 21, 19, and 25 is 22.)

I am having trouble. I can get it where it does the average of all numbers, but I can't get it to where it does the evens and odds and outputs them like (The average of 23, 21, 19, and 25 is 22). Can somebody please help.

Here is the code I have so far:

Code:
import java.util.Scanner; class EvenOddAlex { public static void main(String args[]) { Scanner scan = new Scanner(System.in); double avg; double sum = 0; int num; int count = 0; System.out.println("How many numbers? "); int n = scan.nextInt(); for(int i = 1; i <= n; i++) { System.out.println("Enter a number"); num = scan.nextInt(); { count = count + 1; sum = sum + num; } } avg = sum/n; System.out.println("The average of " + n + " is " + avg); } }
__________________
I am a Java n00b.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-11-2008, 04:30 PM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 251
sanjeevtarar is on a distinguished road
Hello Zebra,

Please check with this code.

Code:
import java.util.Scanner; class EvenOddAlex { public static void main(String args[]) { Scanner scan = new Scanner(System.in); double avgEven,avgOdd; double sumEven = 0,sumOdd=0; int num; int countEven = 0,countOdd=0; int val = 2; System.out.println("How many numbers? "); int n = scan.nextInt(); for(int i = 1; i <= n; i++) { System.out.println("Enter a number"); num = scan.nextInt(); { if(num%val==0){ countEven = countEven + 1; sumEven = sumEven + num; } else{ countOdd = countOdd + 1; sumOdd = sumOdd + num; } } } avgEven = sumEven/countEven; avgOdd = sumOdd/countOdd; System.out.println("The average of Even is : " + avgEven); System.out.println("The average of Odd is : " + avgOdd); } }

sanjeev
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-16-2008, 03:51 PM
Zebra's Avatar
Member
 
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
Zebra is on a distinguished road
Thank you so much!
__________________
I am a Java n00b.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Integers (averages and remainders)...need help Zebra New To Java 4 04-16-2008 03:26 PM
Accessing boolean Values of another values in one class. a_iyer20 Advanced Java 4 04-15-2008 03:04 PM
How to get the User Name JavaBean Java Tips 0 10-04-2007 11:35 PM
Append text inputed in the textfield into a TextArea romina AWT / Swing 1 08-07-2007 07:35 AM
Help with removing somthing once inputed. fegiflu New To Java 8 07-27-2007 12:58 AM


All times are GMT +3. The time now is 06:46 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org