simple question about my code

Thread Solved

Join Date: Sep 2009
Posts: 30
Reputation: shroomiin is an unknown quantity at this point 
Solved Threads: 0
shroomiin shroomiin is offline Offline
Light Poster

simple question about my code

 
0
  #1
22 Days Ago
Hello everyone. I am required to write a simple code that reads 10 numbers and displays the largest and last number entered. I thought i would be able to do this easily but for some reason the numbers wont get assigned to the variables like i thought.

  1. import java.util.Scanner;
  2. public class LargestNumber {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. int largest=0;
  7. int number=0;
  8. int counter=1;
  9.  
  10. Scanner input=new Scanner (System.in);
  11.  
  12. System.out.println("Please enter a number: ");
  13. number=input.nextInt();
  14.  
  15. number=largest;
  16. counter++;
  17.  
  18. while (counter <= 10){
  19.  
  20. System.out.println("Enter another number: ");
  21. number=input.nextInt();
  22. if (number>largest)
  23. number=largest;
  24.  
  25. counter++;
  26.  
  27. }
  28. System.out.println("Last number entered was: "+ number+".");
  29. System.out.println("The largest number entered was: "+ largest+".");
  30. }
  31.  
  32. }

thats what i made, which i thought would work fine...but then the output says the largest and number variables are equal to 0. I set them to 0 at the top, but how come they arnt assigned the new numbers when the while loop executes?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster
 
3
  #2
22 Days Ago
Maybe you want to reverse line 23?
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 30
Reputation: shroomiin is an unknown quantity at this point 
Solved Threads: 0
shroomiin shroomiin is offline Offline
Light Poster
 
0
  #3
22 Days Ago
ah i knew it had to be something small and stupid...thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC