| | |
simple question about my code
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 30
Reputation:
Solved Threads: 0
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.
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?
Java Syntax (Toggle Plain Text)
import java.util.Scanner; public class LargestNumber { public static void main(String[] args) { int largest=0; int number=0; int counter=1; Scanner input=new Scanner (System.in); System.out.println("Please enter a number: "); number=input.nextInt(); number=largest; counter++; while (counter <= 10){ System.out.println("Enter another number: "); number=input.nextInt(); if (number>largest) number=largest; counter++; } System.out.println("Last number entered was: "+ number+"."); System.out.println("The largest number entered was: "+ largest+"."); } }
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
- simple question about VB code used is SQL Reporting services (VB.NET)
- simple question (Python)
- help! simple question (Python)
- simple question (C++)
- This has to be a simple question to answer. (Visual Basic 4 / 5 / 6)
- can some one make me a simple perl code (Perl)
- Simple Counter Code (ASP)
Other Threads in the Java Forum
- Previous Thread: Drwaing Circles?
- Next Thread: catch without try
| Thread Tools | Search this Thread |
-xlint add android api applet application applications array arrays automation bank bi binary blackberry bluetooth chat class client code compile compiler component database development digit eclipse equation error event fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying pearl problem program programming project qt recursion scanner screen scrollbar server set sms sort sorting spamblocker sql sqlserver string superclass swing system text-file thread threads tree variablebinding windows xor






