方法参数传递的案例
package testone;public class HelloWorld { public static void main(String[] args) { //打印数组的内容 int[] arr = {1, 3, 4, 2}; printArray(arr); System.out.println("-------------------"); int[] arr2 = null; printArray(arr2); System.out.println("-------------------"); int[] arr3 = {}; System.out.println(arr3.length); //0 printArray(arr3); } public static void printArray(int[] arr) { System.out.print("["); if (arr != null && arr.length > 0) { for (int i = 0; i < arr.length; i++) {// if (i== arr.length-1){// System.out.println(arr[i]);// }else {// System.out.println(arr[i]+",");// } System.out.print(i == arr.length - 1 ? arr[i] : arr[i] + ","); } } System.out.println("]"); }}
package testone;public class MethodTest4 { public static void main(String[] args) { //查询数组的索引并返回,数据不存在返回-1 int[] arr = {3, 5, 8, 9, 1}; int index = searchIndex(arr, 7); System.out.println(index); } //定义一个方法,参数接受数组和要查询的数据,返回int类型 public static int searchIndex(int[] arr, int data) { //找出这个数的索引 for (int i = 0; i < arr.length; i++) { if (arr[i] == data) { return i; } } //查无此元素 return -1; }}
package testone;public class CompareArr { public static void main(String[] args) { //比较任意两个数组的值是否相等,相等返回true int[] arr11 = {1, 55, 3}; int[] arr22 = {1, 5, 3}; System.out.println(compare(arr11, arr22)); } //定义一个数组,参数传两个数组,返回类型是布尔类型 public static boolean compare(int[] arr1, int[] arr2) { //判断两个数组的内容是否一样 if (arr1.length == arr2.length) { for (int i = 0; i < arr1.length; i++) { if (arr1[i] != arr2[i]) { return false; } } //遍历结束发现没有不相等的,返回true return true; } else { return false; } }}
return跳出当前方法,立即结束所在方法的执行
package testone;public class ReturnDemo { public static void main(String[] args) { //return关键字的作用 chu(10, 0); } public static void chu(int a, int b) { if (b == 0) { System.out.println("分母不能是0"); return; //跳出当前方法,立即结束所在方法的执行 } int c = a / b; System.out.println("结果是" + c); }}
package testone;import java.util.Scanner;public class TestDemo1 { public static void main(String[] args) { //录入信息调用方法 Scanner sc = new Scanner(System.in); System.out.println("请输入机票原价:"); double price = sc.nextDouble(); System.out.println("请输入月份:"); int month = sc.nextInt(); System.out.println("请输入舱位类型(头等舱,经济仓):"); String type = sc.next(); double rs = cacl(price, month, type); System.out.println("您当前购买机票的价格" + rs); } public static double cacl(double money, int month, String type) { //判断月份是淡季还是旺季 //区间判断使用if if (month > 5 && month <= 10) { //遇到判断值匹配使用switch switch (type) { case "经济舱": money *= 0.85; break; case "头等舱": money *= 0.9; break; default: System.out.println("你输入的舱位不正确"); money = -1; //当前无法计算价格 } } else if (month == 11 || month == 12 || (month >= 1 && month <= 4)) { switch (type) { case "经济舱": money *= 0.65; break; case "头等舱": money *= 0.7; break; default: System.out.println("你输入的舱位不正确"); money = -1; //当前无法计算价格 } } else { System.out.println("月份有问题"); money = -1; } return money; }}
package testone;public class TestDemo2 { public static void main(String[] args) { //求101到200之间的素数 for (int i = 101; i < 200; i++) { //信号位标记 boolean flag = true; //判断遍历的这个数是否是素数 for (int j = 2; j < i / 2; j++) { if (i % j == 0) { flag = false; break; } } //是素数则输出 if (flag) { System.out.print(i + "t"); } } }}