Results 1 to 5 of 5
- 02-06-2013, 10:21 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 4
- Rep Power
- 0
Is there an order in which the Java codes need to be placed? viewed from top to botto
I am studying a tutorial and there was an excercise concerning Loops. I found out that if I placed my code like this it works:
package Userinput;
import java.util.Scanner;
public class Loops {
public static void main (String arg []){
int loopVal;
int end =11;
int answer=0;
int calculation=0;
Scanner read = new Scanner(System.in);
System.out.println("which table do you want?");
calculation = read.nextInt();
for(loopVal=1;loopVal<end;loopVal++){
answer = loopVal*calculation;
System.out.println(loopVal +" times "+calculation+" = "+answer);
}
}
}
But if I placed it like this it doesn't work:
package Userinput;
import java.util.Scanner;
public class Loops {
public static void main (String arg []){
int loopVal;
int end =11;
int answer=0;
int calculation=0
calculation = read.nextInt();
Scanner read = new Scanner(System.in);
System.out.println("which table do you want?");
for(loopVal=1;loopVal<end;loopVal++){
answer = loopVal*calculation;
System.out.println(loopVal +" times "+calculation+" = "+answer);
}
}
}
So I was wondering is there an order in which the Java codes need to be placed? e.g. first give datatypes (int, double etc) and then Scanner codes etc. etc.
Does anybody know?
thanks Honijahu
- 02-06-2013, 10:31 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Is there an order in which the Java codes need to be placed? viewed from top to b
Of course it doesn't work because you forgot a semi-colon in your second version (so it doesn't even compile).
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-06-2013, 10:45 AM #3
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: Is there an order in which the Java codes need to be placed? viewed from top to b
Hi Honijahu, welcome.
To answer your question, the order doesn't matter as long as the variable is declared before it is used and is in scope of where it it being used. By scope I'm refering to variables being declared in another method for example.
int calculation = read.nextInt();
The above is also a valid statement. I would not necessarily apply the same thinking to variables with loops as this would mean you are redeclaring the variable each time it is being called.
Regards.
- 02-06-2013, 11:03 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Is there an order in which the Java codes need to be placed? viewed from top to b
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-06-2013, 11:38 AM #5
Senior Member
- Join Date
- Oct 2010
- Posts
- 316
- Rep Power
- 3
Re: Is there an order in which the Java codes need to be placed? viewed from top to b
I see the confusion.
The position in this case is irelevant as the variable x is declared and initialized prior to its use. I should have added that although it looks like it is declared after its use just by looking at the code, is not how the JVM understands it.
For the OP I was looking at it from a beginners view.
Java Code:System.out.println(x); int x = 11
The first section of code is invalid whist the second works.Java Code:int x = 11; System.out.println(x);
Regards.
Similar Threads
-
Windows7 issue -charts get cropped when viewed in PDF format
By Nimali in forum Java AppletsReplies: 0Last Post: 02-22-2011, 10:55 AM -
where java codes to be run?
By samira_fk222 in forum New To JavaReplies: 1Last Post: 01-03-2011, 09:37 PM -
How load other java codes
By pizzadude223 in forum Java AppletsReplies: 35Last Post: 07-22-2010, 04:09 PM -
java codes
By Balajee in forum AWT / SwingReplies: 1Last Post: 09-30-2008, 05:04 PM -
Font display of applets that are viewed on different platforms
By willemjav in forum Java AppletsReplies: 3Last Post: 07-06-2008, 12:35 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks