For the example, i have a 20 lines long code. Java is now reading 8 line. The question is: how to jump between lines of code? (ex. 8 to 3). b:frusty:
Printable View
For the example, i have a 20 lines long code. Java is now reading 8 line. The question is: how to jump between lines of code? (ex. 8 to 3). b:frusty:
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)
Whoever can give me example how to jump between lines of code?Quote:
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.
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.
YesQuote:
Are you talking about a goto statement that would allow you to jump from one line to another?
Quote:
Post some sample code that shows what you are talking about.
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.");
}
}
}
}
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.
Yes. You can use loop + break / continue to instead of GOTO .
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;//<-------------------
}
}
}
}
Ksharp