Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
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 07-23-2007, 07:34 PM
Member
 
Join Date: Jul 2007
Posts: 11
fegiflu is on a distinguished road
Help with setting number as even or odd
i have an input method to fill an array
it finds the total and the average of the numbers inputed, but i need a way for it to list out the even number and the odd numbers that were inputed, and then find the average of the even numbers and the odd numbers. can anyone help? here what i have so far

Quote:
public class SimplebutmethodicalArray2 {

public static void main(String[] args) {
int[] NumberList = new int[25];
double Sum = 0, Average;
int Count, Size, evennumbsum = 0, evenaverage;

Size = FillList(NumberList, 25);


for(Count = 0; Count < Size ; Count++)
System.out.println("NumberList[" + Count + "] = "+ NumberList[Count]);

for(Count = 0 ; Count < Size ; Count++)
Sum += NumberList[Count];
System.out.println("Total = " + Sum);
Average = Sum / Count;
System.out.println("Average = " + Average);


}

public static int FillList(int[] List, int MaxSize)
{
int K;
System.out.println("Enter Values Below, -99 to Stop");
for(K = 0 ; K < MaxSize ; K++)
{
System.out.print("Enter ");
List[K] = SimpleIO.inputInt();
if(List[K] == -99)
break;
}
return K;
}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-23-2007, 10:13 PM
Member
 
Join Date: Jul 2007
Posts: 39
christina is on a distinguished road
is it what you want?
Code:
public class SimplebutmethodicalArray2 { public static void main(String[] args) { int[] NumberList = new int[25]; /*evenaverage: average of the even numbers oddaverage: average of the odd numbers evennumbsum: total of the even numbers oddnumbsum: total of the odd numbers eventcount= counts the even numbers oddcount= count the odd numbers */ double Sum = 0, Average,evenaverage,oddaverage,evennumbsum = 0, oddnumbsum=0; int Count, Size; int evencount=0, oddcount=0; Size = FillList(NumberList, 25); for(Count = 0; Count < Size ; Count++) System.out.println("NumberList[" + Count + "] = "+ NumberList[Count]); for(Count = 0 ; Count < Size ; Count++) Sum += NumberList[Count]; //this check if it is even. if ((NumberList[Count]% 2)==0){ //it is even evennumbsum+= NumberList[Count]; evencount++; }else{//it is odd oddnumbsum+= NumberList[Count]; oddcount++; } System.out.println("Total = " + Sum); Average = Sum / Count; System.out.println("Average = " + Average); System.out.println("Total of even = " + evennumbsum); evenaverage= evennumbsum/evencount; System.out.println("Average of even = " + evenaverage); System.out.println("Total of odd = " + oddnumbsum); oddaverage= oddnumbsum/oddcount; System.out.println("Average of even = " + oddaverage); } public static int FillList(int[] List, int MaxSize) { int K; System.out.println("Enter Values Below, -99 to Stop"); for(K = 0 ; K < MaxSize ; K++) { System.out.print("Enter "); //List[K] = SimpleIO.inputInt(); if(List[K] == -99) break; } return K; } }
If it isn't, just tell me and I do it again
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-24-2007, 06:56 AM
Member
 
Join Date: Jul 2007
Posts: 11
fegiflu is on a distinguished road
Im having a little trouble. it doesn't seam to work >.<

Quote:
Enter Values Below, -99 to Stop
Enter 1
Enter 2
Enter 3
Enter 4
Enter 5
Enter -99
NumberList[0] = 1
NumberList[1] = 2
NumberList[2] = 3
NumberList[3] = 4
NumberList[4] = 5
Total = 15.0
Average = 3.0
Total of even = 0.0
Average of even = NaN
Total of odd = -99.0
Average of even = -99.0
whats wrong? >.<
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-24-2007, 06:13 PM
Member
 
Join Date: Jul 2007
Posts: 39
christina is on a distinguished road
I'm sorry, write
Code:
if ((NumberList[Count]% 2)==0){ //it is even evennumbsum=evennumbsum+NumberList[Count]; evencount++; }else{//it is odd oddnumbsum=oddnumbsum+ NumberList[Count]; oddcount++; }
instead of:

Code:
if ((NumberList[Count]% 2)==0){ //it is even evennumbsum+= NumberList[Count]; evencount++; }else{//it is odd oddnumbsum+= NumberList[Count]; oddcount++; }
good luck!!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-24-2007, 06:48 PM
Member
 
Join Date: Jul 2007
Posts: 11
fegiflu is on a distinguished road
i still get the same error >.<
i don't think that portion of the code even runs.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-24-2007, 06:54 PM
Member
 
Join Date: Jul 2007
Posts: 32
valery is on a distinguished road
what's the problem?
Doesn't it recognize >.< ? I don't understand
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-24-2007, 07:00 PM
Member
 
Join Date: Jul 2007
Posts: 11
fegiflu is on a distinguished road
well i put

Quote:
if ((NumberList[Count]% 2)==0){
//it is even
evennumbsum=evennumbsum+NumberList[Count];
evencount++;
System.out.println(NumberList[Count] + "is even");
}else{//it is odd
oddnumbsum=oddnumbsum+ NumberList[Count];
oddcount++;
System.out.println(NumberList[Count] + "is odd");
}
do see if it gets that far and it doesn't print anything
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-24-2007, 07:03 PM
Member
 
Join Date: Jul 2007
Posts: 8
momo97 is on a distinguished road
Try this:


public class SwitchingYard {

public static void main(String[] args) {
int Value;

System.out.print("Enter A Number: ");
Value = SimpleIO.inputInt();

System.out.println("Felix is gay");

}
}
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-24-2007, 07:07 PM
Member
 
Join Date: Jul 2007
Posts: 11
fegiflu is on a distinguished road
I got it! with a little bit of tweaking i got it to work =]

Quote:
public class SimplebutmethodicalArray2 {

public static void main(String[] args) {
int[] NumberList = new int[25];

double Sum = 0, Average,evenaverage,oddaverage,evennumbsum = 0, oddnumbsum=0;
int Count, Size, evencount=0, oddcount=0;



Size = FillList(NumberList, 25);


for(Count = 0; Count<Size ; Count++)
System.out.println("NumberList[" + Count + "] = "+ NumberList[Count]);

for(Count = 0 ; Count<Size ; Count++)

.

if ((NumberList[Count]%2)== 0){

evennumbsum = evennumbsum + NumberList[Count];
evencount++;
System.out.println(NumberList[Count] + " is even");
}else{
oddnumbsum = oddnumbsum + NumberList[Count];
oddcount++;
System.out.println(NumberList[Count] + " is odd");
}

for(Count = 0 ; Count<Size ; Count++)
Sum += NumberList[Count];
System.out.println("Total = " + Sum);
Average = Sum / Count;
System.out.println("Average = " + Average);

System.out.println("Even total = " + evennumbsum);

evenaverage= evennumbsum/evencount;
System.out.println("Even average = " + evenaverage);

System.out.println("Odd total = " + oddnumbsum);

oddaverage= oddnumbsum/oddcount;
System.out.println("Odd Average = " + oddaverage);



}

public static int FillList(int[] List, int MaxSize)
{
int K;
System.out.println("Enter Values Below, -99 to Stop");
for(K = 0 ; K < MaxSize ; K++)
{
System.out.print("Enter ");
List[K] = SimpleIO.inputInt();
if(List[K] == -99)
break;
}
return K;
}
}
heres the code i used.

i moved this
Quote:
for(Count = 0 ; Count<Size ; Count++)
Sum += NumberList[Count];
down to the part were its finding the total and the average, instead of above the place were its finding if its even or odd.

thanks for all the help!
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
Setting the DSN tim Database 1 02-14-2008 10:55 PM
JTextArea setting newtojava7 New To Java 1 01-29-2008 03:57 AM
Help with setting up please BlitzA New To Java 6 12-29-2007 01:54 PM
Setting cookies in JSP Java Tip Java Tips 0 12-10-2007 06:33 PM
Setting currency Java Tip Java Tips 0 11-16-2007 03:08 PM


All times are GMT +3. The time now is 05:19 PM.


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