very simple mistake, you were setting the assigning values to the variables improperly
such as "largest=second;"
this sets the value of variable second as the variable largest its suppose to be the other way around :) "second=largest"
Code:
import java.io.*;
public class Assignment4
{
public static void main(String args[]) throws IOException
{
BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many numbers do you have?>");
int n = Integer.parseInt(input.readLine());
int x=0;
int largest=0;
int second=0;
int third=0;
while(x<n)
{
System.out.println("Input number "+ (x+1) + ">");
int i = Integer.parseInt(input.readLine());
if(i>largest) //changed here a bit
{
third=second;
second=largest;
largest=i;
}
else if(i>second) //changed here a bit
{
third=second;
second=i;
}
else if(i>third) //changed here a bit
{
third=i;
}
x+=1;
}
System.out.println(" the three largest numbers are: " + largest + "," + second + "," + third);
}
}
Bookmarks