public static void main(String[] arg0) {
System.out.println(“Hello”);
}复制代码
方法的概念
方法用于包裹一段代码,若是须要执行这段代码能够直接调用这个方法。
方法的定义与调用
定义带有返回值的方法,调用方法获取m的n次方的结果,并输出:
public static void main(String[] arg0) {
int[] ary = {10, 20, 30};
modifyAry(ary);
System.out.println(Arrays.toString(ary));
}
public static void modifyAry(int[] ary){
ary[0] = 666;
}
public static void main(String[] arg0) {
int[] ary = {10, 20, 30};
modifyAry(ary);
System.out.println(Arrays.toString(ary));
}
public static void modifyAry(int[] ary){
ary = new int[]{666, 20, 30};
}复制代码
做业
编写一个方法getCM(int m, int n),接受m,n两个整数参数,求其最小公倍数并返回。
main方法中在控制台输入m与n,而后调用该方法得到最小公倍数并输出。
public class Homework1 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("请输入m:");
int m = console.nextInt();
System.out.println("请输入n:");
int n = console.nextInt();
int result = getCM(m, n);
System.out.println("最小公倍数是:"+result);
}
public static int getCM(int m, int n){
请实现方法
}
}复制代码
2.18位身份证验证算法,18位身份证号的最后一位是经过前17位计算得出,算法以下:
-
把身份证号前17位的每一位分别乘以不一样的系数,前17位中每一位数字对应的系数以下:7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2
-
把每一位数字与系数的乘积结果相加求和。
-
把上一步的结果除以11,余数只多是0,1,2,3,4,5,6,7,8,9,10这11个数字。
-
这11个数字分别对应的最后一位字符是: 1 0 X 9 8 7 6 5 4 3 2 (若是余数为2,则身份证最后一位字符则为X)
要求:编写一个方法verifyID(String id),接收一个18位身份证号字符串,通过运算后返回true(该身份证号合法)或者false(身份证号不合法)。
public class Homework2 {
public static void main(String[] args) {
String code = "37132319920531721X";
boolean ok = verifyID(code);
if(ok){
System.out.println("身份证号合法");
}else{
System.out.println("身份证号不合法");
}
}
public static boolean verifyID(String id){
请实现方法
}
}复制代码
能够将答案代码,写入留言区,代码要不断的编写才会培养“码感”的呦!你不关注一下吗?