1、利用方法重载,写三个方法,分别求出int类型绝对值,float类型绝对值double类型绝对值
public class ChongZai {
public static int zhanHuan(int n)//重载int
{
if(n<0)//判断
n*=-1;
return n;
}
public static float zhanHuan(float n)//重载float
{
if(n<0)
n*=-1;
return n;
}
public static double zhanHuan(double n)//重载double
{
if(n<0)
n*=-1;
return n;
}
public static void main(String[] args) {
System.out.println(zhanHuan(-1));
System.out.println(zhanHuan(-1f));
System.out.println(zhanHuan(-1.0));
}
}
2、将下面给定的数组转置输出
例如 原数组: 1,2,3,4,5,6
转置之后的数组: 6,5,4,3,2,1
public class ZhanZhi{
public static int[] zhanZhi(int[] a)
{
int k=a.length;//获组长
int[] b=new int[k];
for(int i=0;i<k;i++)
{
b[i]=a[k-1-i];//转置
}
return b;
}
public static void main(String[] args) {
int[] a= {1,2,3,4,5,6};
a=zhanZhi(a);
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]);//输出
}
}
}
4、现在有如下2个数组
数组A: “1,7,5,7,9,2,21,13,45”
数组B: “2,5,8,14,21”
将俩个数组合并为数组C,按顺序排列输出
public class Copy {
public static void main(String[] args) {
int[] a= {1,7,5,7,9,2,21,13,45};
int[] b= {2,5,8,14,21};
int c=a.length+b.length;//声明并定义数组d
int[] d=new int[c];
System.arraycopy(a, 0, d, 0, a.length);//copy数组a
System.arraycopy(b, 0, d, a.length, b.length);//copy数组b
for(int i=0;i<c;i++)
{
System.out.print(d[i]+" ");
}
}
}