Rookie Question! Help Appreciated

The purpose of this code: after typing 5 integers to then display the largest and smallest one.

Im not sure where to go from here.

This is my Current Code:

Code :

import java.util.Scanner;
public class Comparison
{
public static void main( String[] args )
{
Scanner input = new Scanner( System.in );
int number1;
int number2;
int number3;
int number4;
int number5;
System.out.print( "Enter First Integer: " );
number1 = input.nextInt();
System.out.print( "Enter First Integer: " );
number2 = input.nextInt();
System.out.print( "Enter Third Integer: " );
number3 = input.nextInt();
System.out.print( "Enter Fourth Integer: " );
number4 = input.nextInt();
System.out.print( "Enter Fifth Integer: " );
number5 = input.nextInt();
if ( number1 > number2 )
System.out.printf( "%d == %d\n", number1, number2 );
if ( number1 < number2 )
System.out.printf( "%d < %d\n", number1, number2 );
}
}

Re: Rookie Question! Help Appreciated

Where to go from here... well what are the options? Where is here?

What does the code do now? What should it do next?

Re: Rookie Question! Help Appreciated

Well right now it doesn't consider all 5 numbers, I want it to take the 5 numbers I input and then tell me the largest and smallest one of the 5 :)

Re: Rookie Question! Help Appreciated

Then I would say the next step would be to make the code consider all 5 numbers.

How would you do it without a computer?

What steps would you take, specifically. Write down each thing you must do to find the answers yourself. Each number you look at, each number you compare to, etc...

When you have a list of steps organized in a start to finish order, then you can start working on the code.

Re: Rookie Question! Help Appreciated

I have no idea what you are saying :P I'm not sure how to make it consider all 5 numbers, never done it before.. that's why i'm asking..

Re: Rookie Question! Help Appreciated

I am saying do not worry about how "it" will solve the problem. I am asking how YOU would solve the problem.

15 84 20 13 68 43

In reality there are two problems to solve;

1) find the highest number

2) find the lowest number

Work on one problem at a time. How would you find the highest number?

Re: Rookie Question! Help Appreciated

The way i'd find the highest number compare one number to all the others. That's what i'm trying to code too, but not sure how to do that exactly

Re: Rookie Question! Help Appreciated

Where is your list of steps to compare one number to all others?

There are many ways to do this, and we do not want to do it for you. This is for you to learn, but we will help you translate your solution into code.

Re: Rookie Question! Help Appreciated

Is 84 > 15

Is 84 > 20

Is 84 > 68

Is 84 > 13

Is 84 > 68

Is 84 > 43

My list

--- Update ---

I know this... But i dont know how to write the code to make it check for multiple numbers, not just 1..

Re: Rookie Question! Help Appreciated

Okay, so far so good.

Have a look at what you have done so far.

First you selected a number, (by default just the first one in line)

Then you compared this number to the next number in line. For this to happen you had to remember which number you were going to compare to the next.

Then you repeat this process for each of the remaining numbers.

-the following still needs to be done-

Somewhere along the way, if a larger number is found, you will have to remember that number instead. When you reach the end of the set of numbers, the number being compared should be the largest. Problem of finding the largest number is solved.

This means you may use a variable like largestNumberSoFar to keep track of the current largest number as you go.

Give it a shot and see what you come up with

Re: Rookie Question! Help Appreciated

I dont know how to make it remember a number.. Like i said im very new..

--- Update ---

Like I know the process, I just don't know what to use, that's my original question :P

Re: Rookie Question! Help Appreciated

Then what is this:
Code :

int number1;
int number2;
int number3;
int number4;
int number5;

and this:
Code :

number1 = input.nextInt();
number2 = input.nextInt();
number3 = input.nextInt();
number4 = input.nextInt();
number5 = input.nextInt();

Looks like you set up 5 variables of type int, and give all five a value. If you can make the program remember five numbers why can you not make it remember another one?

Re: Rookie Question! Help Appreciated

I realize I need to make the bottom line something that instead of printing the answer, it remember's the number, but im not sure how to do that

Quote:

if ( number1 > number2 )

System.out.printf( "%d > %d\n", number1, number2 );

--- Update ---

Ahhh okay i see

--- Update ---

Hmmm, still kinda confused, something like this?

int number6;

if ( number1 > number2 )

number6 = input.nextInt();

If number 1 > 2 it makes the number 6?

then do number 6 > number 3

If its > 3 it stores it as 7? or would that be incorrect?

Re: Rookie Question! Help Appreciated

Quote:

Originally Posted by

**Knighter**
Like I know the process, I just don't know what to use, that's my original question :P

I understand, but no one can write the code for you, you can read about why here.

This exercise is to get you some practice turning a simple process into code so that you can handle a more complicated process. Work on the idea of breaking the problem down into steps that you can turn into code, that is what it is all about. Every detail matters when you tell a computer to do it for you.

--- Update ---

The pseudo code in post #13 does not match what you had in post #9

(As a side note 'number6' is a poor description of what that number is for)

Why are you getting a new number for number6, that should represent the largest number found so far. In post #9 that value was 84, which was the first number in the set, (number1) and not a new number given by the user.

Re: Rookie Question! Help Appreciated

I know it isn't the same, so how can I learn how to do this, cause I don't know what means to use cause I don't understand this and this isn't helping :P

--- Update ---

The way I was thinking of doing it was something like if ( number1 > number2, number3, number4, number5 )

System.out.printf( "%d > %d\n", number1, number2 );

and repeat it 4 times like so for each number, but that is creating an error, im not sure how to list multiple numbers instead of 1

Re: Rookie Question! Help Appreciated

How about this for pseudo code:

(First, the instructions)

after typing 5 integers to then display the largest and smallest one

set up a place to store 5 integers

get 5 integers from the user

set up a place to store the largest integer

give the largest integer variable the value of the first integer.

compare the largest integer variable to each of the remaining numbers in the set (the other 4)

if a larger number is found, replace the largest integer value with the newly discovered larger value, else do nothing

at the end of the set, the value in the largest integer variable will be the largest number given by the user

Do you follow the idea with this? Any questions?

Re: Rookie Question! Help Appreciated

Yes sort of, your brain works a bit faster than mine with this stuff :) I'm a 4th year business student trying to self teach myself java hehe, these are some excercises I found online, however no solution or explanations are given but I really want to solve this one. My question is though how to find the largest integer of the 5, I know how to store it etc once its found but how do I find it, I only know how to compare 2 numbers, not 6, for example

if ( number1 > number2 )

System.out.printf( "%d > %d\n", number1, number2 );

However I dont know how to make that consider multiple other numbers, not just one.

Re: Rookie Question! Help Appreciated

( number1 > number2, number3, number4, number5 ) Something like this just does not work

Read over the pseudo code in post #16 again and see if you can follow the idea there. It is just a more detailed description of what you had in post #9

You will have to compare them one at a time like you started to do in the original code.

Re: Rookie Question! Help Appreciated

So this is where I got to after some dabbling:

I feel like the way im trying to store it is wrong though

Quote:

import java.util.Scanner;

public class Comparison

{

public static void main( String[] args )

{

Scanner input = new Scanner( System.in );

int number2;

int number3;

int number4;

int number5;

int largestinteger;

System.out.print( "Enter First Integer: " );

largestinteger = input.nextInt();

System.out.print( "Enter Second Integer: " );

number2 = input.nextInt();

System.out.print( "Enter Third Integer: " );

number3 = input.nextInt();

System.out.print( "Enter Fourth Integer: " );

number4 = input.nextInt();

System.out.print( "Enter Fifth Integer: " );

number5 = input.nextInt();

if ( largestinteger < number2 )

largestinteger = input.nextInt()

}

}

--- Update ---

Would it perhaps be

Quote:

if ( largestinteger < number2 )

largestinteger = input.number2()

??

Re: Rookie Question! Help Appreciated

I like it all the way up to:

largestinteger = input.nextInt()

When you do the test largestinteger < number2, if numbr2 turns out to be the larger, then you would assign the value of number2 to largestinteger

largestinteger = number2;

Then largestinteger would in fact hold the largest integer so far, continue to check number3 and number4.

Beware the use of > and <

Re: Rookie Question! Help Appreciated

Nice I got it to work!

So it all works but just to see your opinion on how I could make it better (if i can) feedback is nice

Quote:

import java.util.Scanner;

public class Comparison

{

public static void main( String[] args )

{

Scanner input = new Scanner( System.in );

int number2;

int number3;

int number4;

int number5;

int largestinteger;

System.out.print( "Enter First Integer: " );

largestinteger = input.nextInt();

System.out.print( "Enter Second Integer: " );

number2 = input.nextInt();

System.out.print( "Enter Third Integer: " );

number3 = input.nextInt();

System.out.print( "Enter Fourth Integer: " );

number4 = input.nextInt();

System.out.print( "Enter Fifth Integer: " );

number5 = input.nextInt();

if ( largestinteger < number2 )

largestinteger = number2;

if ( largestinteger < number3 )

largestinteger = number3;

if ( largestinteger < number4 )

largestinteger = number4;

if ( largestinteger < number5 )

largestinteger = number5;

System.out.println( largestinteger );

}

}

Re: Rookie Question! Help Appreciated

Thanks Kewish. Was some hair pulling for sure.