944,093 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 519
  • Java RSS
Nov 5th, 2009
0

simple question about my code

Expand Post »
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.

Java Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
shroomiin is offline Offline
33 posts
since Sep 2009
Nov 5th, 2009
3
Re: simple question about my code
Maybe you want to reverse line 23?
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Nov 5th, 2009
0
Re: simple question about my code
ah i knew it had to be something small and stupid...thanks again.
Reputation Points: 10
Solved Threads: 0
Light Poster
shroomiin is offline Offline
33 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Drwaing Circles?
Next Thread in Java Forum Timeline: catch without try





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC