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

Thread Starter
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
 

jiml8

Guest
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.
 
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.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top