Results 1 to 9 of 9
 07232007, 07:34 PM #1Member
 Join Date
 Jul 2007
 Posts
 11
 Rep Power
 0
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
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;
}
}
 07232007, 10:13 PM #2Member
 Join Date
 Jul 2007
 Posts
 39
 Rep Power
 0
is it what you want?
Java 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; } }
 07242007, 06:56 AM #3Member
 Join Date
 Jul 2007
 Posts
 11
 Rep Power
 0
Im having a little trouble. it doesn't seam to work >.<
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
 07242007, 06:13 PM #4Member
 Join Date
 Jul 2007
 Posts
 39
 Rep Power
 0
I'm sorry, write
Java Code:if ((NumberList[Count]% 2)==0){ //it is even evennumbsum=evennumbsum+NumberList[Count]; evencount++; }else{//it is odd oddnumbsum=oddnumbsum+ NumberList[Count]; oddcount++; }
Java Code:if ((NumberList[Count]% 2)==0){ //it is even evennumbsum+= NumberList[Count]; evencount++; }else{//it is odd oddnumbsum+= NumberList[Count]; oddcount++; }
 07242007, 06:48 PM #5Member
 Join Date
 Jul 2007
 Posts
 11
 Rep Power
 0
i still get the same error >.<
i don't think that portion of the code even runs.
 07242007, 06:54 PM #6Member
 Join Date
 Jul 2007
 Posts
 32
 Rep Power
 0
what's the problem?
Doesn't it recognize >.< ? I don't understand
 07242007, 07:00 PM #7Member
 Join Date
 Jul 2007
 Posts
 11
 Rep Power
 0
well i put
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");
}
 07242007, 07:03 PM #8Member
 Join Date
 Jul 2007
 Posts
 8
 Rep Power
 0
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");
}
}
 07242007, 07:07 PM #9Member
 Join Date
 Jul 2007
 Posts
 11
 Rep Power
 0
I got it! with a little bit of tweaking i got it to work =]
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;
}
}
i moved this
for(Count = 0 ; Count<Size ; Count++)
Sum += NumberList[Count];
thanks for all the help!
Similar Threads

Setting the DSN
By tim in forum JDBCReplies: 1Last Post: 02142008, 10:55 PM 
JTextArea setting
By newtojava7 in forum New To JavaReplies: 1Last Post: 01292008, 03:57 AM 
Help with setting up please
By BlitzA in forum New To JavaReplies: 6Last Post: 12292007, 01:54 PM 
Setting cookies in JSP
By Java Tip in forum Java TipReplies: 0Last Post: 12102007, 06:33 PM 
Setting currency
By Java Tip in forum Java TipReplies: 0Last Post: 11162007, 03:08 PM
Bookmarks