KaiquanMah's picture
Create Week 2: Methods, strings and lists/1. Write the main method
a760458 verified
raw
history blame
1.49 kB
In Java, all subprograms are called methods.
In programming, a method is generally a subprogram bound to a class.
In Java, all subprograms are methods because all program code is always written inside a class.
methods of procedure type, that is, methods that have NO RETURN VALUE.
In this case, we are actually talking about an empty return value - the type of return value is VOID.
public static void methodName(<list of parameters>) {
<method structure>
}
public class Example {
public static void main(String[] args) {
System.out.println("Hello everyone!");
}
}
The main method has a special meaning in Java programs:
if a class has a MAIN METHOD, it is AUTOMATICALLY EXECUTED 1ST when the class is executed.
=================================
1. Write the main method
Write the main method in the program. The method should print a table of six multiples exactly as shown in the example output below.
Output:
Table of six multiples
1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
4 x 6 = 24
5 x 6 = 30
6 x 6 = 36
7 x 6 = 42
8 x 6 = 48
9 x 6 = 54
10 x 6 = 60
import java.util.Random;
public class Test{

public static void main(String[] args){
System.out.println("Table of six multiples");
for (int i = 1; i <= 10; i++) {
System.out.println(i + " x 6 = " + (i * 6));
}
}
}
Table of six multiples
1 x 6 = 6
2 x 6 = 12
3 x 6 = 18
4 x 6 = 24
5 x 6 = 30
6 x 6 = 36
7 x 6 = 42
8 x 6 = 48
9 x 6 = 54
10 x 6 = 60