Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Goto like thing

  1. #1
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Goto like thing

    If any of you ever have tried batch file prgramming you will probably know the command "Goto" I need to know if there are some thing like "Goto in the java language.
    Please tell me.

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Goto like thing

    No, there are no more goto statements in java (see 2.2.6 in The Java Language Environment). but even if goto would be available I would never use it.

  3. #3
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    But If I have a point in my code I want to go back to what can I then do ?

  4. #4
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    public class goto {
    public static void main(String[] args){
    //The point I want to go back to.
    System.out.println("I am the special point!");
    try{
    Thread.sleep(10000);
    }catch(Exception e){}
    System.out.println("Now I want to go back");

    I know how to use while but when while is used I cant use it again... :(
    please help!!!

  5. #5
    Iron Lion is offline Senior Member
    Join Date
    Nov 2010
    Posts
    210
    Rep Power
    5

    Default Re: Goto like thing

    The break and continue statements work like a limited version of goto (see the link j2 posted).

    It was kept out of Java for a reason: there's never a good reason to use goto in a language that supports it. Anything that can be achieved with a goto would be better done with loops and functions/methods.

    I know how to use while but when while is used I cant use it again... :(
    Incorrect. You can nest as many while loops as the stack allows.
    Last edited by Iron Lion; 12-27-2011 at 04:07 PM.

  6. #6
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    Thanks you.
    What I meant by the while loop whas that you can't go back to a loop and use it as I know...

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Goto like thing

    Quote Originally Posted by EStrike View Post
    Thanks you.
    What I meant by the while loop whas that you can't go back to a loop and use it as I know...
    Your assumptions are wrong, but without more information or code, there's no way to help show you the correct way to fix this.

    You must understand why Java and most modern programming languages don't have a goto. goto type statements are easily misused and in the past have lead to incomprehensible code with bugs that are almost impossible to get rid of, so called "spaghetti code". There is almost always a better way to do what you want than to use goto. If you give us more information, perhaps we can show you.

  8. #8
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    I am working on a text based rpg game (my first).
    code:
    import java.util.Scanner;
    import java.util.Random;

    public class HeroBridge{
    public static void main(String[] args){

    Scanner text = new Scanner(System.in);
    Random rand = new Random();
    int Change = rand.nextInt(100);

    String name;
    System.out.println("What is your name?");
    name = text.nextLine();

    System.out.println("Hello "+name);
    System.out.println("and welcome to HeroBridge");
    try{
    Thread.sleep(5000);
    }catch(Exception e){}
    System.out.println("Befor you start I'll give you a sword");
    System.out.println("*You have got a small sword*");
    try{
    Thread.sleep(5000);
    }catch(Exception e){}
    System.out.println("And I'll give you som gold coins to!");
    System.out.println("*You have got 100 gold coins*");
    }
    }

    It is supposed to be many cities as you can go back and forth from and therefore I need some kind of point you can return to.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Goto like thing

    I fear that you may be confusing virtual location (where the protagonist is currently located in the virtual world) with code location (what line of code is currently running) and need to understand that the two are completely separate and unique things.

    I would try to re-conceive your project in an object-oriented way where you create classes to represent things (such as a Location class, a City class (extends Location), a Room class, a Person class, a Sword class), and that you create a Map class to hold locations, and then allow a Person to move on the Map.

  10. #10
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    So I have to make a class for all in the game ?

  11. #11
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    And how do I make a class for each thing ?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Goto like thing

    You'll want to learn about object-oriented design concepts and how they relate to Java. I suggest you check out Joshua Bloch's book, Effective Java, for a decent introduction on the subject.

  13. #13
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    Do I really have to but a book to learn this ?

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Goto like thing

    Quote Originally Posted by EStrike View Post
    Do I really have to but a book to learn this ?
    I think for most of us it didn't require just a book, it required many books to learn to code well.

  15. #15
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    But do I really need to use a class to each thing if I'll make a text bassed rpg game ?

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Goto like thing

    Quote Originally Posted by EStrike View Post
    But do I really need to use a class to each thing if I'll make a text bassed rpg game ?
    If you want to make a very simple and limited toy game with one character and a very limited number of items, locations and behaviors, then no. If you want to create anything more, then yes.

  17. #17
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    I want to make a game where I can travel from city to city earn money get a simple job and get weapons and so on.

  18. #18
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

    Default Re: Goto like thing

    Do I need to make classes to each thing for that ?

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Goto like thing

    Quote Originally Posted by EStrike View Post
    Do I need to make classes to each thing for that ?
    Again, if you want to create a simple toy program, then no. But if you want to create anything more complex, and if you want to learn to program in Java in general, you'll need to learn OOP (object-oriented programming) concepts.

  20. #20
    EStrike is offline Member
    Join Date
    Dec 2011
    Posts
    21
    Rep Power
    0

Page 1 of 2 12 LastLast

Similar Threads

  1. JTable whole thing changes
    By MarkusHendersonicus in forum New To Java
    Replies: 3
    Last Post: 01-12-2011, 05:09 PM
  2. Trying to understand the whole 2D thing.
    By xael in forum Java 2D
    Replies: 4
    Last Post: 10-06-2010, 02:41 AM
  3. please i need help with a simple thing
    By zohdi in forum New To Java
    Replies: 28
    Last Post: 05-30-2010, 02:41 AM
  4. Bring back the GOTO ;-)
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 09-28-2009, 11:02 AM
  5. goto statement
    By bugger in forum New To Java
    Replies: 2
    Last Post: 12-12-2007, 04:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •