# problem with my code (FOR LOOPS) neeed help plZ

• November 24th, 2011, 10:51 PM
jessy sonita
problem with my code (FOR LOOPS) neeed help plZ
hello dear , good morning ^^
i have a problem with my code !~
coz i have a task to prepare a code for a program to find the two largest values of the 10 values entered.

PHP Code:

```     Scanner input = new Scanner( System.in );        int largest1  ;// largest number    int number; // user input    int counter; // number of values entered    int largest2 = 0 ;// the second largest number     System.out.println( "Enter the number 1:" );     number = input.nextInt();          largest1=number;     counter=2;     while(counter<=10){                      System.out.println( "Enter the number "+counter+":" );                 number = input.nextInt();                    if((number>largest1))              largest2=largest1;             largest1=number;                    counter++ ;     }       System.out.println("largest1 "+largest1 );       System.out.println(largest2 +"largest2");                       }    }  ```
what the wrong with my code

for example ;;
if i entered for example 10 numbers
1 4 6 88 22 99 11 101 99 26
and then the output will be
largest1 = 101
largest2 = 99

help me , i'm beginner in java ^^
• November 25th, 2011, 05:37 AM
KevinWorkman
Re: problem with my code (FOR LOOPS) neeed help plZ
Quote:

Originally Posted by jessy sonita
if i entered for example 10 numbers
1 4 6 88 22 99 11 101 99 26
and then the output will be
largest1 = 101
largest2 = 99

Isn't that exactly what it should output?
• November 25th, 2011, 05:44 AM
jessy sonita
Re: problem with my code (FOR LOOPS) neeed help plZ
yes , it should out put
101
99

what's the problem with the code plz
• November 25th, 2011, 05:59 AM
KevinWorkman
Re: problem with my code (FOR LOOPS) neeed help plZ
Quote:

Originally Posted by jessy sonita
yes , it should out put
101
99

what's the problem with the code plz

You tell us. What does it do instead of what you expect it to do? Have you stepped through this with a debugger, or at least added some print statements? Recommended reading: http://www.javaprogrammingforums.com...t-println.html

Hint: Curly brackets are your friend.
• November 25th, 2011, 06:05 AM
jessy sonita
Re: problem with my code (FOR LOOPS) neeed help plZ
if i wanna use the while loop as i put in the code ~!
how will it be ?
• November 25th, 2011, 06:15 AM
KevinWorkman
Re: problem with my code (FOR LOOPS) neeed help plZ
Quote:

Originally Posted by jessy sonita
if i wanna use the while loop as i put in the code ~!
how will it be ?

I don't understand your question. You already do have a while loop in your code.
• November 25th, 2011, 06:53 AM
jessy sonita
Re: problem with my code (FOR LOOPS) neeed help plZ
Big problem with an assignment.
For some reason, when I input 0, 6, 2, 3, 4, 5, 6, 7, 8, and 9 into this program, it tells me my largest is 9 and second is 0.
Any Java experts here that can tell me how to fix this to have 8 as the result of 2nd largest?
• November 25th, 2011, 06:56 AM
jessy sonita
Re: problem with my code (FOR LOOPS) neeed help plZ
give any code that tells me the 2nd largest number is 8 not 0
• November 25th, 2011, 09:45 AM
KevinWorkman
Re: problem with my code (FOR LOOPS) neeed help plZ
Quote:

Originally Posted by jessy sonita
give any code that tells me the 2nd largest number is 8 not 0

That's not how this works. That's academic dishonesty and is extremely rude.

I already gave you several hints that you have ignored. Respond to them, and we'll go from there. Use a debugger or follow the directions in the link I gave you.
• December 7th, 2011, 12:46 PM
Hemambara
Re: problem with my code (FOR LOOPS) neeed help plZ
Keep the statements under if condition in braces. Like below

if((number>largest1)){
largest2=largest1;
largest1=number;
}
• December 7th, 2011, 12:52 PM
KevinWorkman
Re: problem with my code (FOR LOOPS) neeed help plZ

Consider this a friendly warning.
• December 12th, 2011, 10:22 AM
2by4
Re: problem with my code (FOR LOOPS) neeed help plZ
The logic is wrong, not just the if statement.

Ironically, correcting the if statement may give the correct result for the particular case, but the code will be wrong.

A good idea is to also test with numbers sorted in the opposite direction, like:

1, 9, 8, 7, 6, 5, 4, 3, 2, 0

That will go wrong even if you fix the if