Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Smallest and largest numbers

843789Jan 22 2010 — edited Jan 22 2010
Ok, I have already done a search, and haven't found what I'm looking for. I've just started learning programming with Java, How to Program, 7e and one of the exercises asks me to find the largest and the smallest numbers using only what has been covered so far. I have looked around and learned how to do this, but I am using the "&&" operator, which hasn't been covered yet. I also have to write another program that finds the largest and smallest out of five numbers. Anyone have any ideas? My code works, but like I said, it goes beyond what I learned in the chapter.

Relevant things that we have covered:
Printing text to the screen
Arithmetic operators
Order of operations
Equality operators (== and !=)
Relational operators (<, >, <=, >=)
Basic variables
Basic If statements (single-selection statements, no code blocks or else statements)
// Exercise217.java
// Write an application that inputs three integers from the user and displays
// the sum, average, product, smallest and largest of the numbers. 
import java.util.Scanner;

public class Exercise217
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);

        int x, y, z;
        int sum, average, product;

        System.out.print("Enter first integer: ");
        x = input.nextInt();

        System.out.print("Enter second integer: ");
        y = input.nextInt();

        System.out.print("Enter third integer: ");
        z = input.nextInt();

        sum = x    z;
        product = x < <strong>*  z;
        average = sum / 3;

        System.out.printf("Sum is %d\nAverage is %d\nProduct is %d\n", sum, average, product);

        if (x > y && y > z)
            System.out.printf("%d is the largest\n%d is the smallest", x, z);
        if (x > z && z > y)
            System.out.printf("%d is the largest\n%d is the smallest", x, y);

        if (y > x && x > z)
            System.out.printf("%d is the largest\n%d is the smallest", y, z);
        if (y > z && z > x)
            System.out.printf("%d is the largest\n%d is the smallest", y, x);

        if (z > x && x > y)
            System.out.printf("%d is the largest\n%d is the smallest", z, y);
        if (z > y && y > x)
            System.out.printf("%d is the largest\n%d is the smallest", z, x);
        }
    }
        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 19 2010
Added on Jan 22 2010
8 comments
2,752 views