Results 1 to 10 of 10
Thread: How to use "return"?
- 06-26-2012, 04:39 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
How to use "return"?
Hey there. Currently very new to Java, took classes but the teacher could be better so...trying to train and learn with some self-study!
Anyway, I have a Switch with several options. What I want is to return to the initial menu if a wrong option is given!
Example:
I want to return to the bolded part!public class MenuPrincipal {
public static void main(String[] args) {
System.out.println("Menu");
System.out.println("Choose an option:");
System.out.println("1-First option");
System.out.println("2-Second option");
System.out.println("0-Exit");
Scanner s = new Scanner(System.in);
int opcao = s.nextInt();
switch ( opcao ) {
case 1:
System.out.println("Option 1");
break;
case 2:
System.out.println("Option 2");
break;
case 0:
System.out.println("Goodbye");
System.exit(2);
default:
System.out.println("Invalid Option. ");
return;
- 06-26-2012, 04:42 PM #2
Re: How to use "return"?
The return statement is for exiting the currently executing method. I think what you want to do is to loop back to an earlier section of code within a method. If you put the code you want to repeat inside a loop, then at any point inside that loop you can "return" to the beginning of the loop by using the continue statement.
If you don't understand my response, don't ignore it, ask a question.
- 06-26-2012, 04:49 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Re: How to use "return"?
Ahhh ok! So the return statement is only used to exit the executing code, with some sort of answer, is that it?
By loop, do you mean while, if, etc?
- 06-26-2012, 04:57 PM #4
Re: How to use "return"?
return exits the currently executing method (possibly with a value) returning to the caller of the method.
for and while are two kinds of loops. The if statement is not a loopIf you don't understand my response, don't ignore it, ask a question.
- 06-26-2012, 08:39 PM #5
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: How to use "return"?
I'll give you a little clue just to get idea how it could be done. For example:Anyway, I have a Switch with several options. What I want is to return to the initial menu if a wrong option is given!
Java Code:public static void main(String[] args) { int opcao = 0; while(!(opcao == 0)) { // your code here opcao = s.nextInt(); // your code here } }
- 06-26-2012, 08:47 PM #6
Re: How to use "return"?
@cselic Did you test your code? How many times will the code inside the while() loop execute?
If you don't understand my response, don't ignore it, ask a question.
- 06-26-2012, 09:21 PM #7
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: How to use "return"?
^You're right, my previous code is not good. Here is the better one:
Java Code:public static void main(String[] args) { int opcao = -1; while(!(opcao == 0)) { // your code here opcao = s.nextInt(); // your code here } }
- 06-26-2012, 09:33 PM #8
Re: How to use "return"?
while(!(opcao == 0)) {
Why use both ! and ==
why not just use != and make it clearer and easier to understand?
while(opcao != 0) {If you don't understand my response, don't ignore it, ask a question.
- 06-26-2012, 10:06 PM #9
Senior Member
- Join Date
- Apr 2010
- Location
- Belgrade, Serbia
- Posts
- 278
- Rep Power
- 4
Re: How to use "return"?
^Norm you're right. That's much better.
Java Code:public static void main(String[] args) { int opcao = -1; while((opcao != 0)) { // your code here opcao = s.nextInt(); // your code here } }
- 06-27-2012, 02:55 PM #10
Member
- Join Date
- Jun 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
DecimalFormat fromat method return junk chacter , formate with this value "NaN"
By yatin.baraiya in forum Advanced JavaReplies: 4Last Post: 06-07-2012, 09:24 AM -
java error "invalid method declaration;return type required"
By intercroc in forum New To JavaReplies: 17Last Post: 05-28-2012, 08:30 PM -
Simple "Global" function to return random int
By mac666 in forum New To JavaReplies: 2Last Post: 01-10-2012, 03:13 PM -
[SOLVED] Why does the compiler return "not a statement" for this method body please?
By trueblue in forum New To JavaReplies: 3Last Post: 05-25-2009, 08:50 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks