-2

Extremely new to Java, have just started using it in the last 2 weeks in school, we've been given the assignment to make a program to determine whether 5 given intergers are even or odd and then give an output to look like this Even: 2 Odd: 3 We need to use the modulo operator and switch statements, and i'm just not sure how to go about this, any help would be appreciated.

John Alex
  • 53
  • 7
  • Check that out: http://stackoverflow.com/questions/21837208/check-if-a-number-is-odd-or-even-in-python – Fusseldieb Oct 08 '16 at 17:54
  • 2
    You can learn about the switch statement by Googling "java tutorial switch". You could try the same for "modulus", but you can find the official Oracle tutorial with "java tutorial operators". – ajb Oct 08 '16 at 17:57
  • What does [modulo](https://en.wikipedia.org/wiki/Modulo_operation) do? (different to [modulus](https://en.wikipedia.org/wiki/Modulus)) – Boris the Spider Oct 08 '16 at 17:57
  • 3
    Have you atleast tried anything first? I mean just googling for "java modulo operator" should get you there... – Mad Matts Oct 08 '16 at 17:59
  • 1
    I assure you will learn if you practice... please post the code for your best attempt. Thanks – lrnzcig Oct 08 '16 at 18:00
  • I understand the modulo and how to use it (equal to 0 is even etc) its just the part after the modulo, how do I actually count which numbers are even and which are odd? – John Alex Oct 08 '16 at 18:05

3 Answers3

1

Let us say you got 5 numbers. Create the array for them and then loop through it. Check every element if the remainder of division by 2 is 0 or 1.

int even = 0;
int odd = 0;

int[] array = new int[5];

for (int i=0; i<array.length; i++)
    if (array[i]%2 == 0)
        even++;
    else
        odd++;
Wojtek
  • 1,288
  • 11
  • 16
  • Thanks but we haven't even learned anything about arrays or loops – John Alex Oct 08 '16 at 18:09
  • @Alex so check my updated answer. If you have any question - ask. If you do not want to use arrays or loops, you can do it manually. For example, you have 5 numbers: `int a = 1`, `int b = 2`, `int c = 3`, `int d = 4`, `int e = 5`. You will have to check the remainder for every of them, for example: `if (a%2 == 0) even++; else odd++;` `if (b%2 == 0) even++; else odd++;` `if (c%2 == 0) even++; else odd++;` `if (d%2 == 0) even++; else odd++;` `if (e%2 == 0) even++; else odd++;`. – Wojtek Oct 08 '16 at 18:10
  • Yes, that gave me the output I needed, Thank you very much. Stupidly I was using if, else if instead of just if else. – John Alex Oct 08 '16 at 18:22
0

You need a loop, inside distinguish if odd or even (switch), modulo as part of the condition and counters. Think about what the second argument of modulo must be to find out if odd or even.

If you have written code and it does not work you can extend your question and get further help.

mm759
  • 1,404
  • 1
  • 9
  • 7
0

Hope it helps

import java.util.Scanner;
public class Out {
    public static void main(String[]args){
        int even=0;
        int odd=0;
Scanner scan= new Scanner(System.in);
int num =0;
while(true){
    System.out.println("Enter a number or -99 to quit :");
    num=scan.nextInt();
     if(num==-99){
         System.out.println("Even: "+even+" odd: "+odd);
        break;
        }
    if(num%2==0){
        even++;
    }
    else if(num%2!=0){
        odd++;
    }

    }
    }
}