Hellooo guys

could any one help me pllz
i have a final exam tmw :(

and the exam is

Write a program that read 5 integers numbers and print the largest and smallest numbers By using IF statements


7 Years
Discussion Span
Last Post by ajst

I doubt anyone will help you if you don't show actual effort. Try to do it and post the code.


Actually i don't know how to answer this java question
i tried with my self and i cant complete the program
take a look here

here is the codes

btw im using J developer

import javax.swing.JOptionPane;

public class Class1 {

public static void main ( String args[] ){ 

    int num1,num2,num3,num4,num5;
            num1=Integer.parseInt(JOptionPane.showInputDialog("Enter Number 1"));
            num2=Integer.parseInt(JOptionPane.showInputDialog("Enter Number 2"));
            num3=Integer.parseInt(JOptionPane.showInputDialog("Enter Number 3"));
            num4=Integer.parseInt(JOptionPane.showInputDialog("Enter Number 4"));
            num5=Integer.parseInt(JOptionPane.showInputDialog("Enter Number 5"));

           System.out.println("num1 is larger");

then i stooped :(

i dont know wht next step to complete this program


Why do'nt you use Array instead of using five variables.
int num[5];
int max, min
Use a loop and scan all values
initialise max and min variable to num[0]
now run a loop and compare all values of array num[5] with both variables..


You only need probably three variables, try to read a variable and then process it. you would grab the first number and insert it as the value of the smallest and largest variables. Then grab the next number and compare it against a smallest and a largest variable and replace that variable with the read variable if it's smaller or larger. Loop through it until you've read five numbers and you will have found your largest and smallest.

Technically your start would work, but you would have to repeat it for every variable, then repeat it the same number of times for less than stopping when you found them both.

However that is very inefficient and nobody would do it and I think your professor wouldn't give you a very good grade anyway.

P.S. - an array works but if you can't handle this without an array I figured you're way out of reach of being able to use an array

Edited by rapture: n/a



the teacher told us
just using the IF else statements :S

i dont know whats the problem


i will tray and do my best to find the solution then i will come back

but pllz
if any one knows how to answer this q plzz post the codes here


We know the code but are not going to just give it to you, it's in your best interest. And if you've got a degree that dealt with any type of programming you should be adept at searching for solutions and working through problems. You might start over and write out pseudocode of what you want the program to do first. We can help you make sure you're on the right track there. After that then you can start with the smallest part and work into the problem instead of tackling the entire thing in one shot. It's not hard and shouldn't take long but a good tactic in any situation where you have to solve problems is to start with what you can figure out and work up from there.


thanq rapture

but pllz if u wanna help me send me the code or post it here


Gladius33, you're not going to learn anything by getting everyone to do the work for you. Here's some pseudocode... if you can't figure it out by reading this, you probably shouldn't be a programmer. Not to mention that it doesn't sound like you even enjoy coding. It sounds like you're just getting a programming degree because it's the cool thing to do right now. Anyway, here's the pseudocode...

1. Take input from user. Input will be 5 integers.
2. Use a FOR loop to take the input 5 times, each time storing the input in an array in the place relative to the iteration number... an example would be:

for(int i=0;i < 5;i++) {
//your code here

...in that loop, store the input in the array[i] place.

3. Using a nested FOR loop, go through the array. In each step in the nested loop, it checks to see if the number in the current place is smaller than the number in the next place. If it is, swap them (you will need a temporary variable for this). The FOR loop outside that is just so it will perform this loop the amount of times that there are places in the array. For example, assuming you have an array with 5 places...

for(int i=0;i<5;i++) {
    for(int j=0;j<5;j++) {
        if(array[j+1] > array[j])
        //swap the variables

I've said more than I should by giving you those FOR loop examples. Now fill in the blanks.


Math majors at my school have to take CPS141 Intro to Java... lets hope this person is a Math Major :D or some other major that needs cps for some reason


Gladius33 if you read rapture post before he explains how to solve the problem basicly in psudo code for you.

it has also been pointed out that you are on the right tracks with the code you have produced but you are only testing 1st number? what happens if the 3rd numbers is actually the largest?

once you have the code for the largest the way you have attempted invert it and you have the code for the smallest.

edit: did't relise there was 2 pages. but luckily my comment still stands and gladius33 has given you even more psedo code.

Edited by ajst: not paying attention

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.