Sinisterly
[Java] Need Help - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Java, JVM, & JRE (https://sinister.ly/Forum-Java-JVM-JRE)
+--- Thread: [Java] Need Help (/Thread-Java-Need-Help)

Pages: 1 2


[Java] Need Help - LightX - 02-23-2013

Hello HC!
Today I have an assignment that I am doing for java, but I need some help. Here is the assignment:



Write a program that asks a user for input. If the user presses enter, the input is echoed back to the command line. The computer asks again for input (and echoes it back) until the user enters "exit" or "quit".

An example output might look like this:

Quote:your input?
aaa
aaa

your input?
Hello
Hello

your input?
exit



I have researched about this...I know that I need to use the scanner utility and use the scanner to set the variable that the user types in and then use a loop to make it keep repeating an if statement or something until it reads the exit. My problem is I don't know what loop to use or anything. I looked up the while, do while, and for loop, but didn't find a way to apply it. I do not want the code just handed to me, I just want to know what to use in order to make this work. Any help would be appreciated. Thank you very much for your time.


[Java] Need Help - LightX - 02-23-2013

Hello HC!
Today I have an assignment that I am doing for java, but I need some help. Here is the assignment:



Write a program that asks a user for input. If the user presses enter, the input is echoed back to the command line. The computer asks again for input (and echoes it back) until the user enters "exit" or "quit".

An example output might look like this:

Quote:your input?
aaa
aaa

your input?
Hello
Hello

your input?
exit



I have researched about this...I know that I need to use the scanner utility and use the scanner to set the variable that the user types in and then use a loop to make it keep repeating an if statement or something until it reads the exit. My problem is I don't know what loop to use or anything. I looked up the while, do while, and for loop, but didn't find a way to apply it. I do not want the code just handed to me, I just want to know what to use in order to make this work. Any help would be appreciated. Thank you very much for your time.


RE: [Java] Need Help - Solixious - 02-24-2013

You could do it with any of the loop, but more preferably while or do-while loop. Take the input from the user inside the loop block, check if it is equal to quit. If yes, then break the loop and exit the program. Else just output what was inputted and carry on with the iteration till the required condition is met.
Smile


RE: [Java] Need Help - Solixious - 02-24-2013

You could do it with any of the loop, but more preferably while or do-while loop. Take the input from the user inside the loop block, check if it is equal to quit. If yes, then break the loop and exit the program. Else just output what was inputted and carry on with the iteration till the required condition is met.
Smile


RE: [Java] Need Help - Jacob - 02-24-2013

https://github.com/oneluckyducky/RockPaperScissors/blob/master/pro/geektalk/rps/RPS.java#L23

This'll explain it all ^-^


RE: [Java] Need Help - Jacob - 02-24-2013

https://github.com/oneluckyducky/RockPaperScissors/blob/master/pro/geektalk/rps/RPS.java#L23

This'll explain it all ^-^


RE: [Java] Need Help - H4R0015K - 02-24-2013

Algorithm:
Quote:1)Ask the user for input.
2)print out the input.
3)Start the while loop with the condition while user input not equal to exit or out.
3a)Ask the user for input.
3b)print out the input.

Whats while loop?
Quote:its a structure which repeats/loop itself until the condition given to it is true.



RE: [Java] Need Help - H4R0015K - 02-24-2013

Algorithm:
Quote:1)Ask the user for input.
2)print out the input.
3)Start the while loop with the condition while user input not equal to exit or out.
3a)Ask the user for input.
3b)print out the input.

Whats while loop?
Quote:its a structure which repeats/loop itself until the condition given to it is true.



RE: [Java] Need Help - Deque - 02-24-2013

Now that I see the question here, I will post, what I wrote LightX in a PM. Maybe others find this useful as well:

A little loop guide:

Use for loops if you have to count or iterate through elements, i.e.
counting from 1 to 10
doing something for all elements in a list

Use a do-while loop if you know that the code within the loop block should be executed at least once, no matter what happens.
i.e.

Code:
do{
   boilWhater()
}while(eggIsNotFinished);

You know that the egg can't be finished unless you started boiling water.

Use a while loop if the code within the block is maybe never executed.
I.e.

Code:
while(sunIsShining) {
   mow();
}

You never even start to mow() if the sun doesn't shine

However, it is not really a problem if you use the "wrong" loop. You can actually use every loop, like you can use every knife for cutting. It is just a matter of readability and convenience to use the right one.

What do you think is the best loop in your case?


RE: [Java] Need Help - Deque - 02-24-2013

Now that I see the question here, I will post, what I wrote LightX in a PM. Maybe others find this useful as well:

A little loop guide:

Use for loops if you have to count or iterate through elements, i.e.
counting from 1 to 10
doing something for all elements in a list

Use a do-while loop if you know that the code within the loop block should be executed at least once, no matter what happens.
i.e.

Code:
do{
   boilWhater()
}while(eggIsNotFinished);

You know that the egg can't be finished unless you started boiling water.

Use a while loop if the code within the block is maybe never executed.
I.e.

Code:
while(sunIsShining) {
   mow();
}

You never even start to mow() if the sun doesn't shine

However, it is not really a problem if you use the "wrong" loop. You can actually use every loop, like you can use every knife for cutting. It is just a matter of readability and convenience to use the right one.

What do you think is the best loop in your case?