One of the most simple loop awnsers
When I first learned Java, I read that so switch to another class (And say your class is Myclass1 and the other one is Myclass2) You did Myclass2.main(args);
But you can also apply that same principle to the same class.
Let's say your in Myclass1, and you need the program to start from the beginning again.
You can just do Myclass1.main(args);
In this example I meant for all the classes to start with
public static void main(String[] args){
}
And thats why you put (args) into the Myclass1.main(args);
If it doesn't have an args in it- well it should work fine.
Please Reply with any bugs or problems with this theory of looping.
I know there are a few problems with it, but overall it solves many looping problems.
Re: One of the most simple loop awnsers
Hello Firestar912!
You mean something like that
Code java:
public class Test {
public static void main(String[] args) {
//do something
Test.main(args);
}
}
The problem with the above code is that the program will never end.
Re: One of the most simple loop awnsers
Yea, that would be the point. Because in a something like a guessing game, you never want the game to end.
Re: One of the most simple loop awnsers
Quote:
Originally Posted by
Firestar912
Yea, that would be the point. Because in a something like a guessing game, you never want the game to end.
"Never" is relative. You want it to stop at some time. You can keep a variable for that reason and evaluate it in a loop. while and do...while loops can be used such way that the loop keeps going until the variable takes an unwanted value.
Re: One of the most simple loop awnsers
It's not a loop, it's recursion. Try inserting a:
Code java:
new Exception("Look at my stack!").printStackTrace()
as the first statement in your main method.
Re: One of the most simple loop awnsers
You can make it so when you win the game, it stops looping using for and else.
Re: One of the most simple loop awnsers
Sure, you can do it recursively...but why? What's the advantage? I can tell you a disadvantage: the stack size is increased every time you recurse, and depending upon the size of the application could cause a huge memory footprint and risk a StackOverflowException. There is a reason why loops exist, and why recursion is better suited for navigating things such as data structures. Here's another: java is object oriented - relying on a main method for application workflow is bad practice and reduces the power object oriented programmed provides.
Quote:
but overall it solves many looping problems
What problems? I would encourage you to describe any advantage to what you suggest, because I fail to see any.
Re: One of the most simple loop awnsers
Haha, you are only pointing out the bad points. If you are just doing a simple text program- like a guessing game- the code will go away after so much code is generated.
Re: One of the most simple loop awnsers
Quote:
Originally Posted by
Firestar912
Haha, you are only pointing out the bad points. If you are just doing a simple text program- like a guessing game- the code will go away after so much code is generated.
Like I said, I encourage you to point out any advantages - which I have yet to see you do...perhaps because I don't understand what you mean by "the code will go away after so much code is generated".
I'd say defeating the purpose of writing code in an object oriented manner is a pretty 'bad point'.