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.
No description
14 Replies
JavaBot
JavaBot6d ago
This post has been reserved for your question.
Hey @Bolo! Please use /close or the Close 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.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Kyo-chan
Kyo-chan6d ago
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.
Wannabree
Wannabree6d ago
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
if (num1 > num){
largest = num1;
}
if (num1 > num){
largest = num1;
}
is accessed and largest is set to the initial num1 value.
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner stdin = new Scanner(System.in);


int largest = Integer.MIN_VALUE;
boolean running = true;


while(running) {
System.out.println("Please provide an integer: ");
int num = stdin.nextInt();

if(num<0) {
running = false;
System.out.println("The largest number is " + largest);
stdin.close();
}else if(num > largest) {
largest = num;
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner stdin = new Scanner(System.in);


int largest = Integer.MIN_VALUE;
boolean running = true;


while(running) {
System.out.println("Please provide an integer: ");
int num = stdin.nextInt();

if(num<0) {
running = false;
System.out.println("The largest number is " + largest);
stdin.close();
}else if(num > largest) {
largest = num;
}
}
}
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()
Bolo
BoloOP6d ago
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
Kyo-chan
Kyo-chan6d ago
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.
JavaBot
JavaBot6d ago
💤 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.
noComment | Kez
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. 🙂
Kyo-chan
Kyo-chan5d ago
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.
Dioxin
Dioxin5d ago
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 is
Bolo
BoloOP5d ago
I will try that, thanks!
JavaBot
JavaBot5d ago
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.
Bolo
BoloOP5d ago
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.
JavaBot
JavaBot5d ago
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.
JavaBot
JavaBot5d ago
Post Closed
This post has been closed by <@578726711814782997>.

Did you find this page helpful?