The first seven chapters introduce the basic syntax of Java, and then introduce another important concept: functions.
Whether you are a novice or an engineer writing a program, you will encounter a very long code with many functions and variables to design. The solution at this time is to cut these requirements into different sub-blocks in the program. These sub-blocks may be the integration of functions or content that is constantly called. We call this a function.
When a problem needs to be solved, the corresponding function can be called directly, and these functions must belong to a class. There are two types of methods according to their usage:
Here, we will continue to introduce several important concepts:
Access_Type Return_Value_Type Function_Name (Parameters){
/*.................
...Description...
.................*/
return value; //Not necessary
}
static void MyFirstMethod(){
System.out.println("Hello World!");
}
→Return Value Type : void
、byte
、short
、int
、long
、float
、double
、String
、char
→Access Type : public
、private
、default
、protected
、static
public class Main{
static int Compare(int Agrade,int Bgrade){
int winner;
winner = Math.max(Agrade,Bgrade);
return winner;
}
public static void main(String[] args){
int[] student={0,0};
int temp,winner;
for(temp=0 ; temp<2 ; temp++){
student[temp] = (int)(Math.random() * 101 );
}
winner = Compare(student[0],student[1]);
System.out.println("The highest grade is : " + winner);
}
}
→存取類型(Access Type) : void、byte、short、int、long、float、double、String、char
→回傳值類型(Return Value Type) : public、private、default、protected、static
上面這兩個舉例,理論上應該是有寫反了