Java While Loop Question
I am struggling with understanding what I am doing wrong when I answer this homework/quiz question. I am supposed to create a while loop that continuously prompts the user for int values until the user inputs a negative value. After the loop terminates, the program will display the largest value entered by the user.
The area I am struggling with is using the if/else if statements to correctly put the larger number in the variable named largest. I have spent hours trying to figure out what I am doing wrong, and would greatly appreciate any insight.

14 Replies
⌛
This post has been reserved for your question.
Hey @Bolo! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
Unfortunately I don't know how did you confuse yourself into this.
You need to know at all times what's the largest value ever entered (and possibly whether any value was registered at all as the largest,) and you need to know temporarily a new number entered.
That's two variables. Not more than that, like you did.
Hey, Java noob here.
your
num1
will always be the first value you enter since it's outside of your while loop.
i don't know why it happens but when you enter a negative number, your first
is accessed and largest
is set to the initial num1
value.
i don't know what your homework/quiz is asking you to do but for this problem you pretty much only need onle else if()
So the place where it's wrong is that it's saying that the largest number entered is 4 when in fact it was 7. It does break when the person enters a negative number, but it should be saying that the largest value is 7
it needs to compare the all the inputs and pick the largest
We know that. The issue is you did not make a program that solves the problem.
It doesn't look like you tried, because it doesn't look like your program makes any kind of sense.
You should try to make programs that make sense, preferably a sense that leads to solving the problem.
We can't know what you were trying to do without explaining yourself. It doesn't look like you wrote something you understood anything about. You must never write any program structures but those that you understand why you write them. Random lines must not exist.
💤
Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use /help ping
.
Warning: abusing this will result in moderative actions taken against you.
Please refrain from posting deliberately unhelpful comments to genuine help requests.
Try to keep this a positive space and encourage new coders instead of talking down to them.
Thank you. 🙂
I do what I can, but this is the core issue, and what's needed is what I said. I did what I could to dance around the subject and no progress was made. They can be left alone, or have their worked done for them, or told it how it is in the hope to fix their approach.
id recommend getting rid of
num1
altogether. instead, just store the "first input" directly in largest
that should simplify the code enough for you to see what the issue isI will try that, thanks!
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Okay so that worked once I removed the num1 and started over with one if statements at a time. I guess I overly complicated it in my head to the point of confusing myself. Thanks to those who actually pointed me in the right direction.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Post Closed
This post has been closed by <@578726711814782997>.