# beginner : wrong code , help me to fix it

Discussion in 'Software Development' started by sanita_sasa, Nov 25, 2011.

Not open for further replies.

Joined:
Nov 25, 2011
Messages:
1
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

2. ### jiml8Guest

Joined:
Jul 2, 2005
Messages:
2,634
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.

As Seen On