Results 1 to 8 of 8
- 06-21-2012, 04:29 PM #1
Member
- Join Date
- Jun 2012
- Posts
- 19
- Rep Power
- 0
- 06-21-2012, 04:32 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,604
- Rep Power
- 5
Re: How to jump between lines of code?
If I understand your question correctly, see the following:
Control Flow Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
- 06-21-2012, 04:39 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 19
- Rep Power
- 0
Re: How to jump between lines of code?
Whoever can give me example how to jump between lines of code?The statements inside your source files are generally executed from top to bottom, in the order that they appear. Control flow statements, however, break up the flow of execution by employing decision making, looping, and branching, enabling your program to conditionally execute particular blocks of code. This section describes the decision-making statements (if-then, if-then-else, switch), the looping statements (for, while, do-while), and the branching statements (break, continue, return) supported by the Java programming language.
- 06-21-2012, 04:42 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,371
- Blog Entries
- 7
- Rep Power
- 17
Re: How to jump between lines of code?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 06-21-2012, 04:45 PM #5
Re: How to jump between lines of code?
Are you talking about a goto statement that would allow you to jump from one line to another?
Java does not have a goto statement. You have to use other types of statements, like a loop.
Post some sample code that shows what you are talking about.If you don't understand my response, don't ignore it, ask a question.
- 06-21-2012, 05:10 PM #6
Member
- Join Date
- Jun 2012
- Posts
- 19
- Rep Power
- 0
Re: How to jump between lines of code?
YesAre you talking about a goto statement that would allow you to jump from one line to another?
Post some sample code that shows what you are talking about.Java Code:import java.util.Scanner; public class ProgramMichs{ public static void main(String [] args){ Scanner scan = new Scanner(System.in); System.out.print("\n-------------------------------"); System.out.print("\nI Witaj w programie MICHS!! I"); System.out.print("\nI-----------------------------I"); System.out.print("\nI Wersja Java 0.01 I"); System.out.print("\nI Dopiero uczę się Javy, więc I"); System.out.print("\nI program wymaga wielu popra- I"); System.out.print("\nI wek. I"); System.out.print("\nI-----------------------------I"); System.out.print("\n[1] Gra w pytania"); System.out.print("\n[2] Autor\n"); System.out.print("[3] Creeper\n"); String in; in = scan.nextLine(); if(in.equals("1")) { System.out.print("\nIle to jest 8 / 2?"); System.out.print("\n4"); System.out.print("\n3"); System.out.print("\n2"); System.out.print("\n1"); System.out.print("\n0\n"); in = scan.nextLine(); if(in.equals("4")) { System.out.print("\nDobrze!"); } else { System.out.print("\nZle! 8 / 2 = 4."); } } if(in.equals("2")) { System.out.print("\nAutor: XicXXl O, pseudo MXXhs"); } if(in.equals("3")) { System.out.print("\nXX XX"); System.out.print("\nXX XX"); System.out.print("\n XX"); System.out.print("\n XXXX"); System.out.print("\n XXXX"); System.out.print("\n X X"); System.out.print("\n SSSSSSS"); System.out.print("\nCo teraz?"); System.out.print("\n[1] Uciekac"); System.out.print("\n[2] Zostac na miejsu\n"); in = scan.nextLine(); if(in.equals("1")) { System.out.print("\nUdalo ci sie zwiac Creeperowi!!"); /*For example, i want to jump from this line of code to 3th line.*/ } else { System.out.print("\nSsSsSs... wiecej juz nie uslyszales bo nie zdazyles."); } } } }
- 06-21-2012, 05:24 PM #7
Re: How to jump between lines of code?
You would wrap the code in a loop and use the continue statement to change the flow of execution back to the beginning of the loop.
If you don't understand my response, don't ignore it, ask a question.
- 06-25-2012, 07:55 AM #8
Banned
- Join Date
- Jun 2012
- Location
- Beijing,China
- Posts
- 34
- Rep Power
- 0
Re: How to jump between lines of code?
Yes. You can use loop + break / continue to instead of GOTO .
Java Code:public static void main(String [] args){ Scanner scan = new Scanner(System.in); for(;;) { //<----------------------- System.out.print("\n-------------------------------"); System.out.print("\nI Witaj w programie MICHS!! I"); System.out.print("\nI-----------------------------I"); System.out.print("\nI Wersja Java 0.01 I"); System.out.print("\nI Dopiero uczę się Javy, więc I"); System.out.print("\nI program wymaga wielu popra- I"); System.out.print("\nI wek. I"); System.out.print("\nI-----------------------------I"); System.out.print("\n[1] Gra w pytania"); System.out.print("\n[2] Autor\n"); System.out.print("[3] Creeper\n"); String in; in = scan.nextLine(); if(in.equals("1")) { System.out.print("\nIle to jest 8 / 2?"); System.out.print("\n4"); System.out.print("\n3"); System.out.print("\n2"); System.out.print("\n1"); System.out.print("\n0\n"); in = scan.nextLine(); if(in.equals("4")) { System.out.print("\nDobrze!"); } else { System.out.print("\nZle! 8 / 2 = 4."); } break;//<------------------- } if(in.equals("2")) { System.out.print("\nAutor: XicXXl O, pseudo MXXhs"); break;//<------------------- } if(in.equals("3")) { System.out.print("\nXX XX"); System.out.print("\nXX XX"); System.out.print("\n XX"); System.out.print("\n XXXX"); System.out.print("\n XXXX"); System.out.print("\n X X"); System.out.print("\n SSSSSSS"); System.out.print("\nCo teraz?"); System.out.print("\n[1] Uciekac"); System.out.print("\n[2] Zostac na miejsu\n"); in = scan.nextLine(); if(in.equals("1")) { System.out.print("\nUdalo ci sie zwiac Creeperowi!!"); /*For example, i want to jump from this line of code to 3th line.*/ } else { System.out.print("\nSsSsSs... wiecej juz nie uslyszales bo nie zdazyles."); break;//<------------------- } } } }
KsharpLast edited by Ksharp; 06-25-2012 at 07:57 AM.
Similar Threads
-
Optimising this simple piece of code (30 lines)
By hvince95 in forum New To JavaReplies: 3Last Post: 01-29-2012, 08:43 AM -
lines truncated in code formatting
By zweibieren in forum Suggestions & FeedbackReplies: 6Last Post: 12-19-2011, 06:13 PM -
Question about some lines of code arrays
By silverglade in forum New To JavaReplies: 9Last Post: 06-09-2011, 04:59 AM -
Please explain these 2 lines of code to me..
By murphaph in forum New To JavaReplies: 10Last Post: 01-19-2010, 02:11 PM -
Unify these 3 lines of code
By ulykidjoe in forum Advanced JavaReplies: 4Last Post: 07-13-2007, 01:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks