This site requires JavaScript, please enable it in your browser!
Greenfoot back
divinity
divinity wrote ...

2016/1/10

need some insight here

divinity divinity

2016/1/10

#
hi I am doing this array program where the user has to enter a set of number which must be terminated by -1 and then it should print the total values or sum or all even numbers and also count all the odd numbers in the set for example it should be like this: enter the numbers, 2,44, 3, 5, 0 sum of even numbers: 46 odd numbers count: 2 my problem is I am getting it to count the even numbers and odd numbers which may sound good but it is not like that. it is not like how it supposed to do. when the user enter a set of numbers all it giving you the amount of odd and even numbers. e.g if you enter 10 numbers it would tell you it have 5 even number and 5 odd numbers when it should add all the even numbers and count the odd numbers remain.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int values[]= {0, 1, 2, 3, 4, 5, 6,7, 8, 9, 10};
         
        int odd =0;
        int even =0;
        int i=0;
        int sum=0;
         
         
         
         
       for(i = 0; i<10; i++)
        {
            System.out.println("please enter number and terminate by -1 ");
            values[i] = userinput.nextInt();
             
            if(values[i]%2==0)
            {
                 
                even = even + 1;
                 
            }
            else if(values[i]%2!=0)
            {
                
                odd = odd+1;;
            }
            sum += even;
            sum += odd;
             
        }
            System.out.println("number of even numbers is "+ even);
            System.out.println("number of odd numbers is "+ odd);
tell me what I am missing here any help would be greatly appreciated. tell me how i should get it to terminate by -1
Super_Hippo Super_Hippo

2016/1/10

#
What are lines 27 and 28 supposed to do? It adds the number of even and odd numbers (which is the same as 'i+1') to 'sum'. To get the sum of the numbers, you have to add 'values(i)' (of course with bracket instead of braces, can't use them outside code tags here) (and if you want to do this for even and odd separately, then you need one variable each (or another array)). I don't know what you mean 'terminate by -1'. If it means that the user should enter a number which is not -1, then it could be done like this I think. (I never used the 'nextInt' method, so I am not sure if it will work.)
1
2
3
4
5
do
{
    values[i] = userinput.nextInt();
}
while (values[i] != -1);
divinity divinity

2016/1/10

#
what i meant by terminate by -1 it mean that the program should run until the user enter -1 for it to stop but i dont know how to get it to do just that
Super_Hippo Super_Hippo

2016/1/10

#
To exit the loop, use:
1
if (values[i] == -1) break;
danpost danpost

2016/1/10

#
Unfortunately, the 'do-while' loop provided by Super_Hippo does not increment the value of 'i' within it, Also, nothing is preventing the user from entering more than 10 numbers; so, even if 'i' was incremented, an error will occur if eleven numbers were input without a '-1' being entered before the eleventh number. There are then two limits to the looping -- a maximum of 10 numbers (limited by the size of the array) or when '-1' is entered (to terminate entering before 10 numbers are entered). The conditional 'break' line given by Super_Hippo will work fine within the loop. The other condition can be that of the 'do-while' loop itself.
Super_Hippo Super_Hippo

2016/1/10

#
danpost wrote...
Unfortunately, the 'do-while' loop provided by Super_Hippo does not increment the value of 'i' within it
That's because there is an error... It should have been:
1
2
//...
while (values[i] == -1);
But now as I know what terminate means, you can disregard this piece of code.
danpost danpost

2016/1/10

#
The word 'terminate' means (1) to stop; or (2) to end. In this case, I was referring to the iteration of the loop being stopped or, more specifically, breaking out of the loop. I was suggesting something like this:
1
2
3
4
5
6
7
do
{
    values[i] = userinput.nextInt();
    if (values[i] == -1) break;
    i++;
}
while (i < 10);
divinity divinity

2016/1/10

#
okay thank you both danpost and super_hippo
You need to login to post a reply.