sy04_2:方法中参数传递的练习

1.  编写一个程序文件KY4_2.java。

2.  程序功能如下:有一个主方法main() 以及一个求平方和的方法int square(int x, int y)。square方法计算参数x 和y的平方和,并返回该平方和值。在主方法main中定义两个整型变量,并给它们赋一个初值,然后使用这两个变量作为实际参数来调用square方法,求出平方和,最后输出平方和。

KY4_2.java代码:

public class KY4_2 {
	public static void main(String[] args) {
		int a = 1;
		int b = 2;
		//square sq = new square(a,b);
		KY4_2 sq = new KY4_2();
		System.out.println("平方和为"+sq.square(a,b));
	}
	int square(int x, int y) {
			int n;
			n = x*x+y*y;
			return n;
	}
}
运行结果:平方和为5

猜你喜欢

转载自blog.csdn.net/qq_40956679/article/details/80731761