I'm currently taking Java I and I have to write a program that reads 5 integers and determines and prints the largest and smallest numbers without using an array. I have to only use the if statements. I can't figure out how to make the if statement apply to 5 variables and manipulate all of them. I'm using the Deitel 8th edition How to Program book. I can't figure out how to make the if statement compare all five. This is due today and I have tried contacting my teacher and a classmate and neither have responded. Here is what I have so far:

import java.util.Scanner;

public class LargestAndSmallestIntegers

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

//set variables
int num1;
int num2;
int num3;
int num4;
int num5;

System.out.println(" Enter first ineger: " ); //prompt
num1 = input.nextInt();

System.out.println(" Enter second integer: " ); //prompt
num2 = input.nextInt();

System.out.println(" Enter third integer: " ); //prompt
num3 = input.nextInt();

System.out.println(" Enter fourth integer: " ); //prompt
num4 = input.nextInt();

System.out.println(" Enter fifth integer: " ); //prompt
num5 = input.nextInt();

if (num1 > num2)
System.out.printf("%d", num1);



Edited 3 Years Ago by happygeek: fixed formatting

You only need two variables, one to hold the largest integer and one to hold the smallest integer. Initially, you should set both the large integer and the small integer to the first value read in. For each of the four values afterwards, you should use your if statements to compare the value to the value in your smallest variable and largest variable. If the new value is smaller, set the 'smallest' variable to it. Etc.

This is what your program would look like in pseudocode:

print("please enter your first value");
int val = getTheIntegerValue();
smallest = val; 
highest = val;

//Now you have to read in 4 more values
for (int count = 0; count < 4; count++){
print ("enter the next value");
int val = getIntValue();
if (val > highest) highest = val;
if (val < smallest) smallest = val;


You'll notice I gave you some code that will work in Java, but I only gave you that code because doing so makes my explanation confusing. Other code is pseudocode, so the idea works, but the exact statements will not work in any language. Look at it because that's what you have to do in Java.

I really appreciate your response. Unfortunately, I am new to programming and per the assignment, I am only able to use the if statement and unable to use an array to obtain the numbers. Any further advice would be appreciated.

There is no array in my pseudocode or in my explanation. Re-read my explanation. All you're doing is prompting for each value, and after each value, checking to see if that value was greater than or less than the previous. Since you're only storing two things, you do not need an array, you only need two variables.

Sorry about the confusion, all I can say is that I'm new. I built the code below and it works, but will not print the if statements. Could there be a configure problem with my netbeans? Can you test this code to see if it prints the statements properly? Thank you for your help.

System.out.println(" Enter an integer: " );

int value = getTheIntegerValue();
int smallest = value;
int highest = value;

for (int count = 0; count < 4; count++)

System.out.println(" Enter next integer: " );
value = input.nextInt();

if (value > highest) highest = val
System.out.printf("Hishest value is %d ", value);
if (value < smallest) smallest = val
System.out.printf("Lowest value is %d ", value);


Edited 3 Years Ago by happygeek: fixed formatting

Use the "code" tag when posting code. I see a mistake in your code (the bracket after the for loop is in the wrong place), but I cannot tell you what line the error is on because you didn't post with code tags.

This article has been dead for over six months. Start a new discussion instead.