注意:
a)方法定义:方法必须先创建
b)方法调用:方法创建后,需要手动使用后,才执行
修饰符 返回值类型 方法名 (形参列表[可以没有形参]) {
方法体(需要执行的功能代码);
return 返回值;[或无返回值,此时返回值类型为void]
}
// 示例:
//public static:修饰符 int:返回值类型 sum:方法名 int a, int b:形参列表
public static int sum(int a, int b){
int c = a + b; //方法体
return c; //c:返回值
}
方法名(实际参数列表)
// 示例
sum(13, 10)
public static void main(String[] args) {
//方法调用
int s1 = sum(100);
int s2 = sum(50);
System.out.println("s1:" + s1 + " s2:" + s2); //s1:5050 s2:1275
}
// 方法定义
public static int sum(int n){
int sum = 0;
for (int i = 1; i <= n; i++) {
sum += i;
}
return sum;
}
//方法调用
judge(2); //2:偶数
judge(5); //5:奇数
// 方法定义
public static void judge(int n){
if (n % 2 == 1){
System.out.println(n + ":奇数");
} else{
System.out.println(n + ":偶数");
}
}
//方法调用
int[] arra = {41,61,55,15,67,48,49};
System.out.println(arrMax(arra));
// 方法定义
public static int arrMax(int[] a){
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (max < a[i]){
max = a[i];
}
}
return max;
}
public static void main(String[] args) {
int sum = add(10, 20);
System.out.println(sum);
}
public static int add(int a, int b){
int c = a + b;
return c;
}
public static void main(String[] args) {
study();
}
public static void sleep(){
System.out.println("sleep");
}
public static void eat(){
System.out.println("eat");
}
public static void study(){
System.out.println("eat");
System.out.println("study");
System.out.println("sleep");
}
实参:调用方法时,实参列表中的参数。是方法外部定义的变量。
形参:定义方法时,形参列表中所声明的参数。
public static void main(String[] args) {
int a = 10;
para(a);
System.out.println("main:" + a);
}
public static void para(int a){
System.out.println("para1:" + a);
a = 20;
System.out.println("para2:" + a);
}
/*
para1:10
para2:20
main:10
*/
int[] b = {12, 20, 23, 14};
para2(b);
System.out.println("main:" + b[0]);
public static void para2(int[] b){
System.out.println("para1:" + b[0]);
b[0] = 15;
System.out.println("para2:" + b[0]);
}
/*
para1:12
para2:15
main:15
*/
//方法调用
int[] arr = {11, 22, 33, 44, 55};
arrList(arr); //[11, 22, 33, 44, 55]
// 方法定义
public static void arrList(int[] arr){
if (arr != null & arr.length > 0){
System.out.print('[');
for (int i = 0; i < arr.length - 1; i++) {
System.out.print(arr[i] + ", ");
}
System.out.println(arr[arr.length-1]+"]");
}
}
//方法调用
int[] arr = {11, 22, 33, 44, 55};
System.out.println(arrIndex(arr,2)); //-1
//方法定义
public static int arrIndex(int[] a, int value){
for (int i = 0; i < a.length; i++) {
if (a[i] == value){
return i;
}
}
return -1;
}
//方法调用
int[] arr = {11, 22, 33, 44, 55};
int[] arr2 = {11, 22, 33, 44, 55};
System.out.println(arrSame(arr, arr2)); //true
//方法定义
public static boolean arrSame(int[] a, int[] b){
if (a.length == b.length) {
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]){
return false;
}
}
return true;
}else {
return false;
}
}
// 方法调用
public static void main(String[] args) {
fire();
fire("japanese");
fire("japanese",5);
}
// 方法定义
public static void fire(){
System.out.println("japanese:1 fire");
// 方法调用 调用其重载方法
fire("japanese");
}
public static void fire(String location){
System.out.println(location + ":" + "1 fire" );
// 方法调用 调用其重载方法
fire("japanese", 1);
}
public static void fire(String location, int num){
System.out.println(location + ":" + num + " fire");
}
注意:
作用
可读性好,方法名称相同提示是同一类型的功能,通过形参不同实现功能差异化的选择,这是一种专业的代码设计。
识别技巧
// 新方法
public static void open(){}
// 方法重载
public static void open(int i){}
// 方法重载
public static void open(int i, int j){}
// 方法重载
public static void open(double i, int j){}
// 方法重载
public static void open(int i, double j){}
// 与上面重复,不区分形参变量名
//public static void open(int a, double b){}
// 新方法 java区分大小写
public static void OPEN(){}
return
可以用在任何方法中,跳出并结束当前方法的执行public static void main(String[] args) {
System.out.println("begin");
//方法调用
chu(2,0);
System.out.println("end");
}
// 方法定义
public static void chu(int a, int b){
if (b == 0){
System.out.println("除数不能为0");
return; //跳出并结束当前方法的执行
}
int c = a / b;
System.out.println("the result is:" + c);
}
/*begin
除数不能为0
end*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("please input the price of ticket:");
float sale = sc.nextFloat();
System.out.println("please input the month of ticket:");
int month = sc.nextInt();
System.out.println("please input the level of ticket(top:1 lower:2):");
int level = sc.nextInt();
System.out.println("the diacount price:" + price(sale, month, level));
}
public static float price(float sale, int month, int level){
if (month >= 5 && month <=10){
switch (level){
case 1:
sale *= 0.9;
break;
case 2:
sale *= 0.85;
break;
default:
System.out.println("the level has wrong");
return -1;
}
}
else if (month == 11 || month == 12 || month >= 1 && month <=4){
switch (level){
case 1:
sale *= 0.7;
break;
case 2:
sale *= 0.65;
break;
default:
System.out.println("the level has wrong");
return -1;
}
}
else{
System.out.println("the month has wrong");
return -1;
}
return sale;
}
public static void main(String[] args) {
int sum = 0;
for (int i = 101; i <= 200; i++) {
if (find(i) == 1){
sum += 1;
}
}
System.out.println('\n'+ "the sum of num is:" + sum);
}
//判断是不是素数
public static int find(int a){
// 标志:1素数 0非素数
int flag = 1;
for (int i = 2; i < Math.sqrt(a); i++) {
if (a % i == 0){
flag = 0; //有其他因子,非素数
}
}
// 素数输出值
if (flag == 1){
System.out.print(a + " ");
}
return flag;
}
public static void main(String[] args) {
String vai = Validnum(5);
System.out.println("Random Vaild Code" + vai);
}
// 产生随机数验证码
public static String Validnum(int len){
// 返回字符串类型 字符串拼接‘ + ’
String vaildStr = "";
Random rd = new Random();
for (int i = 0; i < len; i++) {
// 随机生成数字/大写/小写字母
int type = rd.nextInt(3);
switch (type){
case 0://大写字母(65--65+25) 强制类型转换
char ch = (char)(rd.nextInt(26)+65);
vaildStr += ch;
break;
case 1://小写字母(97--97+25) 强制类型转换
char ch1 = (char)(rd.nextInt(26)+97);
vaildStr += ch1;
break;
case 2:数字(0--9)
vaildStr += rd.nextInt(10);
break;
}
}
return vaildStr;
}
// NU4QK
public static void main(String[] args) {
int[] a = {16, 54, 98};
// 动态初始化数组
int[] b = new int[a.length];
copyArr(a, b);
for (int i = 0; i < b.length; i++) {
System.out.print(b[i] + " ");
}
}
// 复制a数组元素到b数组 将两个数组作为形参,不需要返回值
public static void copyArr(int[] a, int[] b) {
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
}
public static void main(String[] args) {
// 初始化分数数组
int[] s_all = new int[6];
score(s_all);
max_min_avg(s_all);
}
// 获取全部得分 为分数数组复制
public static void score(int[] s){
Random rd = new Random();
for (int i = 0; i < s.length; i++) {
s[i] = rd.nextInt(101);
}
System.out.println("评委打分:");
for (int i = 0; i < s.length; i++) {
System.out.print(s[i] + " ");
}
}
//找出数组最值, 去掉两值后求和求平均
public static void max_min_avg(int[] a){
// 初始设为数组的第一个值
int max = 0;
int min = 0;
//求数组最大最小值的索引
for (int i = 1; i < a.length; i++) {
if (a[max] < a[i]){
max = i;
}
if (a[min] > a[i]){
min = i;
}
}
//求去掉最大值,去掉最小值的所有和
int sum = 0;
System.out.println('\n'+"max:" +max + " min:" + min);
for (int i = 0; i < a.length; i++){
sum += a[i];
}
sum -= a[max];
sum -= a[min];
System.out.println("和为:" + sum);
System.out.println("平均分:" + (sum * 1.0/a.length));
}
比如
----:1 9 8 3
+5:6 14 13 8
%10:6 4 3 8
反转:8 3 4 6
public class JiaMi {
public static void main(String[] args) {
int[] a = new int[4];
ArrGenera(a);
ChangeArr(a);
}
//生成数组,输出
public static void ArrGenera(int[] a){
Scanner sc = new Scanner(System.in);
System.out.println("请输入密码:");
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
System.out.println("原数组为:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
System.out.println();
}
//修改数组
public static void ChangeArr(int[] a){
int[] b = new int[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = a[i] + 5;
b[i] = b[i] % 10;
}
for (int i = 0; i < a.length; i++) {
a[i] = b[a.length -1 - i];
}
System.out.println("加密数组:");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]);
}
}
// 原数组为:
// 8725
// 加密数组:
// 0723
用户输入一组双色球号码;
判断中奖情况
2. 分析
public static void main(String[] args) {
// 初始化中奖号码和猜测号码
int[] trueArr = new int[7];
int[] guessArr = new int[7];
trueNum(trueArr);
guessNum(guessArr);
judge(trueArr, guessArr);
}
// 生成中奖号码
public static void trueNum(int[] a){
Random rd = new Random();
System.out.print("生成中奖号码: ");
for (int i = 0; i < a.length - 1; i++) {
//方法1:
// a[i] = rd.nextInt(33) + 1;
// // 确保中奖号码的各位不相同
// if (i > 0) {
// for (int j = 0; j < i; j++){
// if(a[i] == a[j]){
// a[i] = rd.nextInt(33) + 1;
// j = -1;
// }
// }
// }
//方法2:
while(true){
int temp = rd.nextInt(33)+ 1;
boolean flag = true;//标志位:初始化新加入的号码与之前不相同
// 确保中奖号码的各位不相同
for (int j = 0; j < i; j++) {
if (a[j] == temp){
flag = false;
break;
}
}
//如果新加入的号码与之前不相同,则赋值给数组
if (flag){
a[i] = temp;
}
}
}
a[a.length-1] = rd.nextInt(16) + 1;
printArr(a);
}
// 输出数组
public static void printArr(int[] a){
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
}
// 猜测中奖号码
public static void guessNum(int[] a){
Scanner sc = new Scanner(System.in);
System.out.println("请输入猜测号码(前六位1~33,第七位1~16):");
for (int i = 0; i < a.length; i++) {
System.out.print("第" + (i+1) + "位:");
a[i] = sc.nextInt();
}
System.out.println("猜测中奖号码:");
printArr(a);
}
// 判断中奖情况
public static void judge(int[] a, int[] b){
// 红球蓝球命中数量
int red = 0;
int blue = 0;
for (int i = 0; i < a.length-1; i++) {
if (a[i] == b[i]){
red++;
}
}
if (a[a.length - 1] == b[a.length - 1]){
blue++;
}
// string: + 做连接
switch ("red + '+' + blue"){
case "6+1":
System.out.println("1等奖:1000万元");
break;
case "6+0":
System.out.println("2等奖:500万元");
break;
case "5+1":
System.out.println("3等奖:3000元");
break;
case "5+0":
case"4+1":
System.out.println("4等奖:200元");
break;
case"4+0":
case"3+1":
case"2+1":
System.out.println("5等奖:10元");
break;
case"1+1":
case"0+1":
System.out.println("6等奖:5元");
break;
default:
System.out.println("未中奖!");
break;
}