123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package j1_java_basic.J20250525;
- /**
- * @author WanJl
- * @version 1.0
- * @title Demo06
- * @description
- * @create 2025/5/25
- */
- public class Demo06 {
- /*
- public static void 方法名(参数列表){
- 方法体
- }
- */
- // 定义一个方法,传入1个整数,该方法可以判断这个数是奇数还是偶数。
- public static void m1(int n){
- if (n%2==0){
- System.out.println("偶数");
- }else {
- System.out.println("奇数");
- }
- }
- // 创建一个方法,该方法接收一个整数,可以计算从1到这个整数的累加和。控制台输出
- public static void m2(int n){
- int sum=0;
- for (int i = 1; i <=n; i++) {
- sum+=i;
- }
- System.out.println(sum);
- }
- // 创建一个方法,该方法接收一个数组,可以输出该数组中最大的值。
- public static void getMax(int[] arr){
- int max=arr[0];//先设定一个最大值
- int index=0;
- for (int i = 0; i < arr.length; i++) {
- if (max<arr[i]){
- max=arr[i];
- index=i;
- }
- }
- System.out.println("数组元素最大的是"+max+",它在数组中的索引值是"+index);
- }
- public static void main(String[] args) {
- //调用方法,并且传递参数
- m1(6);
- int[] arr={1,3,4,98,1,6,54,78,89,1,66,654,6,4,8,4};
- getMax(arr);
- }
- }
|