方法练习2

10.6

在java中定义方法:

package lianxi;

public class bo {
public static void main(String[] args) {
int result;

for (int x = 1; x <= 10; x++)
{
result = square(x);
// Math库中也提供了求平方数的方法
// result=(int)Math.pow(x,2);
System.out.println("The square of " + x + " is " + result + "\n");
}
}

// 自定义求平方数的静态方法
public static int square(int y)
{
return y * y;
}
}

运行结果:

 运行结果分析:

程序自定义了一个求平方数的静方法square;

原文地址:https://www.cnblogs.com/092e/p/14142447.html