-1

So, I have already figured out how to calculate the average and number of integers entered; however, I cannot seem to figure out how to figure out the largest, smallest, even and odd numbers. I have tried several things, but it does not work.

Any tips or suggestions? I do not need for you to write anything for me, but a little guidance would be appreciated. (this is for school, do not want to cheat, just need some help).

import java.util.Scanner;

public class Lab4
{
    public static void main(String[] args)
    {


    double large = 0;
    double small = 0;
    double even = 0;
    double odd = 0;
    double foot = 0;
    double ball = 0;
    double eagles = 0;
    System.out.println("Enter positive or negative integers -- enter zero to quit");
    Scanner scan = new Scanner(System.in);
    boolean philly = false;

    while (!philly)
    {
        eagles = scan.nextDouble();
        if (eagles == 0)
        {
            philly = true;
        }
        else
        {
            foot = foot + eagles;
            ball++;
        }
    }

    if (ball > 0)
    {
        System.out.println("The number of integers entered is: " + ball);
        double avg = foot / ball;
        System.out.println("Average of integers: " + avg);
    }
    else
    {
        System.out.println("No data");
        }


    }
}

3 Answers3

1

Tracking max and min is usually done this way:

  • allocate a variable for max; set it to a ridiculously low number (zero, negative, etc).
  • allocate a variable for min; set it to a ridiculously high number.
  • in the loop, put in an if() that sets max to current if current is bigger than max
  • similar if() for min going the other way

Odd and even only makes sense with integer numbers (i.e. convert to int or unsigned first). Use the modulus operator ("odd" means var%1 == 1, etc).

BJ Black
  • 2,483
  • 9
  • 15
  • It's usually easier to check for _even_, though. Why? – Clockwork-Muse Sep 08 '16 at 23:38
  • There are a couple of ways of doing it, but I find the modulus method is best because everyone understands it and it's easy to maintain. var%2==0 is odd, var%2==1 is even. Easy to do and understand. – BJ Black Sep 08 '16 at 23:44
0

for odd and even numbers take a look at this link

for highest and lowes numbers you need to check if the new number is higher/lower then the previous highest/lowest

Community
  • 1
  • 1
aldr
  • 838
  • 2
  • 19
  • 33
0

More detail as requested. I have tried several variations of the code below, but my largest, smallest, even and odd integers are broken.

import java.util.Scanner;

public class Lab4
{
    public static void main(String[] args)
    {


    double large = Integer.MAX_VALUE;
    double small = Integer.MIN_VALUE;
    double evenCount = 0;
    double oddCount = 0;
    double foot = 0;
    double ball = 0;
    double eagles = 0;
    System.out.println("Enter positive or negative integers -- enter zero to quit");
    Scanner scan = new Scanner(System.in);
    boolean philly = false;

    while (!philly)
    {
        eagles = scan.nextDouble();
        if (eagles == 0)
        {
            philly = true;
        }
        else
        {
            foot = foot + eagles;
            ball++;
        }
    }

    if (eagles%2==0)
    {
        evenCount++;
        System.out.println("The number of even integers is: " + evenCount);

    if (eagles%2==1)
    oddCount++;
        System.out.println("The number of odd integers is: " + oddCount);

    if (eagles < small)
        small = eagles;
    System.out.println("The smallest integer entered is: " + small);

    if (eagles > large)
        large = eagles;
        System.out.println("The largest integer entered is: " + large);

    if (ball > 0)

        System.out.println("The number of integers entered is: " + ball);
        double avg = foot / ball;
        System.out.println("Average of integers: " + avg);
    }
    else
    {
        System.out.println("No data");
        }


    }
}