一.函数
1.函数是什么?
函数分为主函数和自己定义的函数,主函数是每个程序都必须拥有的,代码执行从主函数开始,到主函数结束,无论中间进去了多少次其他函数,最终都返回主函数结束。
函数的存在简化了代码,减少了不必要的重复代码,使程序看起来整洁。且使程序容易修改。
2.函数的定义
我们定义函数是这样的格式:public static 函数返回值类型/void (传入的参数类型){}
1.如果不返回值则用void定义函数。
2.返回小数用double/float,返回整数用int/long int,返回真假用boolean。返回字符/字符串用char/String。
3.传入的参数也可以是任何类型。
4.void不能有返回值,其他类型必须有返回值。
3.函数的调用
public class test{
public static void test01(int n)
{
System.out.print(n);
}
public static void main(String []args) {
test01(10);
}
}
在这里我们定义了一个 test01 函数,void类型没有返回值,传入类型为int类型。系统输出这个传入的值。程序的运行从主函数开始,我们在主函数里面调用了这个函数,传入的int类型的值是10,所以我们的输出结果就是10。
public class test{
public static int test01(int n)
{
return n;
}
public static void main(String []args) {
int n = test01(10);
System.out.print(n);
}
}
在这里我们定义了一个返回值为int类型的函数,我们在主函数内给test01函数传入了10,这个函数将给我们的主函数返回一个值,在这里返回了10,然后我们将结果打印。最后运行结果就是:10
public class test{
public static double test01(double n)
{
return n;
}
public static void main(String []args) {
double n = test01(10.0);
System.out.print(n);
}
}
这个程序创建了一个返回值为double类型的函数,在这里我们传入了10.0,函数给主函数返回了10.0,最后输出这个结果。所以最后运行结果就是10.0
public class test{
public static char test01(char n)
{
return n;
}
public static void main(String []args) {
char n = test01('x');
System.out.print(n);
}
}
在这里我们定义了一个返回值为char类型的函数,在这里我们传入了一个char类型的变量'x',最后返回给主函数这个变量,最后输出结果为:x
如果我们函数的返回值为char类型,而我们需要返回的为String类型,则会报错。
public class test{
public static String test01(String n)
{
return n;
}
public static void main(String []args) {
String n = test01("你好");
System.out.print(n);
}
}
我们在这里定义了一个返回值类型为String类型的函数,给这个函数传入了一个字符串“你好”,然后返回这个字符串,最后输出的结果就是:你好
字符可以用字符串的形式返回,字符串不可以用字符的形式返回。
public class test{
public static boolean test01()
{
return true;
}
public static void main(String []args) {
System.out.print(test01());
}
}
在这里定义了一个返回值为boolean类型的函数,我们没有给函数传递变量,boolean类型返回值为true/false,最后我们输出了函数返回的true
4.函数的简单应用
如果我们想写一个程序,返回从a到b所有数字相加之和,我们如果要用函数处理,可以使代码看起来十分简洁。首先我们定义一个sum函数,这个函数里面进行从a到b所有数字相加之和,所以我们这个函数需要一个返回值,且返回值为int类型。我们还需要给这个函数传入两个值,所以我们这样定义函数:public static int sum(int a,int b) 在主函数调用时用int a =sum(a,b);将返回值传入主函数。所以程序这么写:
public class test{
public static int sum (int a,int b)
{
int sum=0;
for (int i = a;i<=b;i++)
{
sum+=i;
}
return sum;
}
public static void main(String []args) {
int a = 1, b = 2;
int c = sum(a,b);
System.out.print(c);
}
}
程序很简单,最后的输出值为3.
5.Java中几种基本函数
1.取绝对值函数。
public class test{
public static void main(String []args) {
int a = 1;
int b = -6;
int c = a+b;
System.out.print(Math.abs(c));
}
}
在这里我们用Math.abs()对c取了绝对值。所以最后输出的结果是|-5|为5
2.指数函数
public class test{
public static void main(String []args) {
int a = 2;
int b = 3;
double c = Math.pow(a, b);
System.out.print(c);
}
}
在这里我们用了math.pow函数计算了a的b次方。因为这个函数计算的是小数类型,所以我们用double定义c,最后输出的结果为:8.0
3.Math.PI函数
public class test{
public static void main(String []args) {
System.out.print(Math.PI);
}
}
输出结果为 3.141592653589793
4.Math.E函数
public class test{
public static void main(String []args) {
System.out.print(Math.E);
}
}
输出结果为:2.718281828459045
5.Math.ceil(int i)函数
public class test{
public static void main(String []args) {
System.out.print(Math.ceil(4.3));
}
}
函数输出比他大的最大整数。如果是整数输出它本身。结果为5.0
6.Math.floor(int i )函数
public class test{
public static void main(String []args) {
System.out.print(Math.floor(4.6));
}
}
输出比他小的最大整数,如果是整数则输出它本身。结果为4.0
7.Math.hypot(x,y)函数
public class test{
public static void main(String []args) {
System.out.print(Math.hypot(2,2));
}
}
此方法返回欧几里德范数 sqrt(x*x + y*y)结果为:2.8284271247461903
8.Math.max(a,b)和Math.min(a,b)函数
public class test{
public static void main(String []args) {
System.out.println(Math.max(2,3));
System.out.println(Math.min(2,3));
}
}
输出结果为3 2 一个返回两个数中最大的,一个最小的。
9.Math.sqrt(a)函数
public class test{
public static void main(String []args) {
System.out.println(Math.sqrt(9));
}
}
输出结果为3.0 是取二次根号下。
10.Math.random()函数
public class test{
public static void main(String []args) {
System.out.println(Math.random());
}
}
输出结果为[0,1)中任意一个数字,取随机数。
11.Math.rint(a)函数
public class test{
public static void main(String []args) {
System.out.println(Math.rint(4.5));
}
}
输出结果为4.0&#x