方法练习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;

posted @ 2020-10-06 10:07  潘福龙  阅读(48)  评论(0编辑  收藏  举报