Print triangle
for (int a= 1; a <= 5; a++) {
for (int b = 5; b>=a ; b--) {
System.out.print(" ");
}
for (int c = 1; c <= a; c++) {
System.out.print("*");
}
for (int d = 2; d <=a ; d++) {
//或者 for (int d = 1; d < a ; d++)
System.out.print("*");
}
System.out.println();
run:
*
***
*****
*******
*********
Detailed Java methods
-
System.out.println(); What is it?
System is a class, out is an object, println is a method (call the println method in the standard output object out in the system class)
-
A Java method is a collection of statements that perform a function together.
The method is an ordered combination of steps to solve a class of problems
Methods are contained in classes or objects
The method is created in the program and is referenced elsewhere
-
The principle of design method: The original intention of a method is a function block, which is a collection of sentence blocks that realize a certain function. When we design a method, it is best to keep the method atomic, that is, a method can only complete one function , which is conducive to our later expansion. *
E.g:
public static void main(String[] args) {
pro(3,4,5);
System.out.println("======================");
int d=pro1(2,3,4);// (2,3,4) 实际参数
System.out.println(d);
System.out.println("======================");
triangle();//使用方法;
System.out.println("======================");
triangle();
}
public static void pro(int a,int b,int c){
System.out.println(a*b*c);}
//(int a,int b,int c)是形式参数
public static int pro1(int a,int b,int c){
return a*b*c;}
public static void triangle(){
for (int a= 1; a <= 5; a++) {
for (int b = 5; b>=a ; b--) {
System.out.print(" ");
}
for (int c = 1; c <= a; c++) {
System.out.print("*");
}
for (int d = 2; d <=a ; d++) {
//或者 for (int d = 1; d < a ; d++)
System.out.print("*");
}
System.out.println();
run:
60
======================
24
======================
*
***
*****
*******
*********
======================
*
***
*****
*******
*********
Method definition and call
- The Java method is similar to the function of other languages. It is a piece of code used to complete a specific function . Generally, defining a method includes the following syntax:
- The method contains a block header and a block body. Here are all the parts of a method:
public static void main(String[] args) {
int a = compare(2,3);
System.out.println(a);
System.out.println("========");
int b = compare(3,2);
System.out.println(b);
System.out.println("========");
int c = compare(2,2);
System.out.println(c);
System.out.println("========");
}
public static int compare(int a,int b) {
int c = 0;
if (a==b){
System.out.println("两个数相等");
return 0;//return除了返回值,还会终止方法。
}
if (a > b) {
c = a;
}
else{
c = b;}
return c;
run:
3
========
3
========
两个数相等
0
========
Method overload
public class A0119CompareTheSize {
public static void main(String[] args) {
int a = compare(2, 3);
System.out.println(a);
System.out.println("========");
int b = compare(3, 2);
System.out.println(b);
System.out.println("========");
int c = compare(2, 2);
System.out.println(c);
System.out.println("========");
//方法调用
double num1 =compare(1.2,2.2,3.3);
System.out.println(num1);
double num2 =compare(2,5,5);
System.out.println(num2);
}
public static int compare(int a, int b) {
int c = 0;
if (a == b) {
System.out.println("两个数相等");
return 0;//return除了返回值,还会终止方法。
}
if (a > b) {
c = a;
} else {
c = b;
}
return c;
}
//方法调用 (调用compare)
public static double compare(double a, double b, double c) {
double d = 0;
if (a == b && b == c) {
System.out.println("三个数相等");
return 0;//return除了返回值,还会终止方法。
}
if (a >=b && a >= c) {
d = a;
}
if (b >= a && b >= c) {
d = b;
}
if (c >= b && c >= a) {
d = c;
}
return d;
run:
3
========
3
========
两个数相等
0
========
3.3
5.0
Command line parameter
- Sometimes you want to send a message to a program when it runs. This is achieved by passing command parameters to the main() function.
public class CommandLine{
public static void main(String args{
}){
for(int i=0;i<args.length;i++){
System.out.println("args["+i+"]:"+args[i]);
}
}
}
variable parameter
- Starting with JDK 1.5, Java supports passing variable parameters of the same type to a method.
- In the method declaration, add an ellipsis (...) after the specified parameter type.
- Only one variable parameter can be specified in a method, and it must be the last parameter of the method. Any ordinary parameters must be declared before it.
public class A0119Parameter {
public static void main(String[] args) {
A0119Parameter ling = new A0119Parameter();
ling.printMax(2.2,1111,4,5,6,7);
System.out.println("===========");
}
//算出哪个数是最大的
public void printMax(double ...a) {
//...a 是可变参数
double b = a[0];
if (a.length == 0){
System.out.println("输入不能为空");
return;}
for (int i =1;i<a.length;i++){
if (a[i] >= b) {
b = a[i];
}
}
System.out.println(b);