Question:
Java Looping Question Help Please!!!!!!!!!!!!?
2010-11-16 11:21:08 UTC
keybd = JOptionPane.showInputDialog("Enter person's age: ");
JOptionPane.showMessageDialog(null, list.displayPeopleWithSameAge(keybd));

*****

Basically, I need to make a workable loop that repeatedly ask the user to "Enter the age" while user does not enter -1. If it's not -1, then it should send the String keybd to second line and print.

But the way I'm coding now.. if user enters 11, then it prints 11, then repeats first line, user enters 22, then THE PROBLEM HERE IS THAT IT PRINTS 11 and 22, while it should only print 22..

Can this be done with few lines of for/while/if statements? THANKS!!!

So correct output should be something like:
First line - user enters 12
Second line - print 12
Repeats first line - user enters 40
Second line - print 40
Repeats first line - user enters -1
Program terminates
Six answers:
cpcii
2010-11-16 11:23:26 UTC
sounds like your adding to the string your saving the user input in, set it to blank after it prints it, or have it reset before you use it:



example:



while input <> -1

input = ""

scan input

print input

end while
dufrene
2016-12-16 10:01:22 UTC
i've got not touched Java in a protracted time, yet I nevertheless bear in mind loops particularly. For loops are count quantity-controlled loops - which potential the loop stops as quickly as that's been by using it a undeniable form of cases (which you identify). For (int x=0; x=9; x++) i think of the commencing up of a for loop regarded some thing like that. the 1st area, x=0, tells you the place the counter variable starts. the 2d area, x=9, tells you whilst the loop desires to give up (whilst x equals 9), and the third area, x++, tells you procedures plenty desires to be further to the counter variable whenever you bypass throughout the loops (x++ is a shortcut for x = x+a million) mutually as and do mutually as loops are adventure controlled (i think of that's what they have been called) variables. which potential the loops do not give up until an adventure occurs. for occasion: mutually as x > 9 (do not bear in mind what mutually as loops gave the impact of in Java) That assertion says that the loop won't give up until x is larger than 9. So someplace interior the loop coding you need to characteristic to x. mutually as x > 9 { y++ x = x + 2 } Do { y++ x = x + 2 mutually as x > 9 mutually as loops and do mutually as loops are diverse because of the fact a mutually as loop is a pre-try (pre-some thing, do not bear in mind) loop and a do mutually as loop is a placed up-try (placed up-some thing, do not bear in mind) loop. this means that throughout the time of a mutually as loop, the computing gadget tests if x>9 until now it is going throughout the loop. And in a do mutually as loop, the computing gadget does not verify until after it has long previous throughout the loop a minimum of as quickly as. int x = 10 mutually as x > 9 { y++ x = x + 3 } in this, x is already better than 9, so it does not bypass throughout the loop in any respect. int x = 10 do { y++ x = x + 3 mutually as x > 9 in this, it does not verify for x until the tip, so it is going throughout the loop one time. i'm hoping that helped!
tyrocx
2010-11-16 11:24:52 UTC
if you are using the standard beginning java class command prompt you will need to use a while loop of while input is not -1. The only way to use a bunch of if then statements is if you are using an event handler such as a user clicks a button on a form.
doyan
2016-12-05 06:40:22 UTC
i've got no longer touched Java in a protracted time, yet I nevertheless undergo in suggestions loops quite. For loops are count quantity-controlled loops - meaning the loop stops as quickly as that's been via it a definite style of situations (which you establish). For (int x=0; x=9; x++) i think of the initiating of a for loop regarded something like that. the 1st section, x=0, tells you the place the counter variable starts off. the 2d section, x=9, tells you while the loop desires to end (while x equals 9), and the 0.33 section, x++, tells you methods lots desires to be further to the counter variable whenever you flow in the time of the loops (x++ is a shortcut for x = x+a million) at the same time as and do at the same time as loops are adventure controlled (i think of that's what they have been called) variables. meaning that the loops do no longer end till an adventure happens. as an occasion: at the same time as x > 9 (do no longer undergo in suggestions what at the same time as loops appeared like in Java) That assertion says that the loop won't end till x is bigger than 9. So someplace in the loop coding you will desire to upload to x. at the same time as x > 9 { y++ x = x + 2 } Do { y++ x = x + 2 at the same time as x > 9 at the same time as loops and do at the same time as loops are distinctive by way of fact a at the same time as loop is a pre-try (pre-something, do no longer undergo in suggestions) loop and a do at the same time as loop is a submit-try (submit-something, do no longer undergo in suggestions) loop. this potential that in the time of a at the same time as loop, the workstation tests if x>9 formerly it is going in the time of the loop. And in a do at the same time as loop, the workstation would not examine till after it has long previous in the time of the loop a minimum of as quickly as. int x = 10 at the same time as x > 9 { y++ x = x + 3 } in this, x is already better than 9, so it would not flow in the time of the loop in any respect. int x = 10 do { y++ x = x + 3 at the same time as x > 9 in this, it would not examine for x till the top, so it is going in the time of the loop one time. i'm hoping that helped!
2010-11-16 11:24:19 UTC
Your almost there. Seens you didnt clear the keyboard cache so it ends up repeating the contents from before.
KARL
2010-11-16 11:23:06 UTC
no


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...