# beginner : wrong code , help me to fix it

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

#### sanita_sasa

hi all ;:

i have an assignment :
if the user input 5 numbers < the programme should tell him which the two largest number from them ::

example ;;
if i enter
1 , 33 , -3 , 0 , 45 ,

the programme will tell me that
largest1 = 45
largest2 = 33

how it will be

?

PHP:
``````	Scanner input = new Scanner( System.in );
int largest2;
int largest1  ;// largest number
int number; // user input
int counter; // number of values entered
// the second largest number
System.out.println( "Enter the number 1:" );
number = input.nextInt();

largest1=number;

counter=2;
while(counter<=5){
System.out.println( "Enter the number "+counter+":" );

number = input.nextInt();
if((number>largest1))
largest1=number;
else if
(number >= largest2 && number <= largest1)
largest2=number;
counter++ ;
}
System.out.println("largest1 :: "+largest1 );
System.out.println("largest2 ::"+largest2 );

}

}``````

that code is done and working ,
but the problem that , when i give the largest2 =0
it make a problem with the result when i put negative integers
,
what should i give the 2n number as a initial value for it

#### jiml8

##### Guest
Why wouldn't you just initialize number2 to the same value as number1 in your first data collection? Do this and then it'll work in all but one case...which is a logic error in your while statement. Initialize number2 the same as number1, find and fix your logic error, and it'll work.

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

As Seen On