1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

beginner : wrong code , help me to fix it

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

Thread Status:
Not open for further replies.
  1. sanita_sasa

    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 ScannerSystem.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. jiml8

    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.
     
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 733,556 other people just like you!

Loading...
Similar Threads - beginner wrong code
  1. MarcoPotatoe
    Replies:
    1
    Views:
    716
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1028379

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice