方法 java

1.定义方法并调用

package day5;

import java.util.Scanner;

public class Method {
    public static void main(String[] args) {
        //方法一:
        int a = 10;
        int b = 20;
        int sum = add(a, b); //接收返回值
        System.out.println(sum);

        //方法二:
        int min = min(11, 20);
        System.out.println(min);

        //方法三:
        print();

        //方法四:
        System.out.println(adds(100));


        方法五:判断一个整数的奇偶性
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您需要判断的数:");
        int n = scanner.nextInt();
        judge(n);

        //方法六:
        int[] Arr = {1, 22, 5, 64, 8};
        int max = getMax(Arr); //括号里不能是一条定义语句
        System.out.println(max);
    }


	//方法写在main方法外,类方法里
    // 修饰符 返回值类型 方法名(形参列表){
    // 方法体
    // return 返回值 它的后面都执行不到,是无效代码
    // }
    //没有返回值,方法类型声明为void,不用return,
    //如果方法也是void类型则主方法调用时不用定义变量来接收直接调用就可以。
    public static int add(int a, int b) {
        return a + b;
    }

    //找最小值
    public static int min(int a, int b) {
        int min = a < b ? a : b;
        return min;
    }

    public static void print() {
        System.out.println("Hello World!");
    }

    //计算1-n相加的和
    public static int adds(int n) {
        int sum = 0;
        for (int i = 0; i < (n + 1); i++) {
            sum += i;
        }
        return sum;
    }

    //判断一个整数的奇偶性
    public static void judge(int n) {
        if (n % 2 == 0) {
            System.out.println("偶数");
        } else {
            System.out.println("奇数");
        }
    }


    //求数组中的最大值
    public static int getMax(int[] Arr) {
        int temp = Arr[0];
        for (int i = 0; i < Arr.length; i++) {
            if (temp < Arr[i]) {
                temp = Arr[i];
            } else {
                temp = temp;
            }
        }
        return temp;
    }
}

在这里插入图片描述

2.判断输入的数是否在数组中

package day5;

import java.util.Arrays;
import java.util.Scanner;

public class Method1 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要查询的数据:");
        int num = scanner.nextInt();
        getIndex(arr, num);
    }

public static void getIndex(int[] arr, int num) {
for (int index = 0; index < arr.length; index++) {
            if (num != arr[index]) {
                if (index < (arr.length - 1)) { //循环判断,如果到最后还不相同就输出不存在,需要对判断的次数进行再次判断
                    continue;
                } else {
                    System.out.println("您输入的数不存在该数组中...");
                }
            } else if (num == arr[index]) {
                System.out.println("该数存在,输入元素索引为" + index);
                break;
            }
        }
    }
}

在这里插入图片描述
在这里插入图片描述

3.方法重载

public class Method2 {
    public static void main(String[] args) {
        int[] Arr = {1, 2, 3};
        int[] Brr = {1, 2, 4};
        judge(Arr, Brr);

        fire();
        fire("某地区");
        fire("某地区", 3);
        fire(3, "某地区");
        fire(3.2, "某地区");
    }

    //方法重载  不能体现在变量名的不同  个数类型顺序
    public static void fire() {
        System.out.println("发射武器");
    }

    public static void fire(String location) {
        System.out.println("向" + location + "发射武器");
    }

    public static void fire(String location, int num) {
        System.out.println("向" + location + "发射" + num + "枚武器");
    }

    public static void fire(int num, String location) {
        System.out.println("向" + location + "发射" + num + "枚武器");
    }

    public static void fire(double num, String location) {
        System.out.println("向" + location + "发射" + num + "枚武器");
    }
}

在这里插入图片描述

package day5;

public class Method3 {
    public static void main(String[] args) {
        playGame("jq");
        playGame("jq", 1);
        playGame(1,"jq");
    }

    public static void playGame(String people) {
        System.out.println("和" + people + "一块玩游戏");
    }

    public static void playGame(String people, int num) {
        System.out.println("和" + num + "只" + people + "一块玩游戏");
    }

    public static void playGame(int num, String people) {
        System.out.println("和" + num + "只" + people + "一块玩游戏");
    }
}

在这里插入图片描述

4. 通过键盘录入两个整数n和m。n代表行数,m代表列数。定义一个方法,方法的功能是打印n行m列的@符号

package day5;

import java.util.Scanner;

public class Practice3 {
    public static void main(String[] args) {
      Scanner scanner = new Scanner(System.in);
        System.out.println("请输入行数:");
        int n = scanner.nextInt();
        System.out.println("请输入列数:");
        int m = scanner.nextInt();
        print(n, m);
    }

    public static void print(int n, int m) {
        for (int i = 1; i <= n; i++) {
            for (int x = 1; x <= m; x++) {
                System.out.print("@");
            }
            System.out.println();
        }
    }
}

在这里插入图片描述

5.四舍五入

package day5;

import java.util.Scanner;

public class Practice7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入您要进行四舍五入的小数:");
        double a = scanner.nextDouble();
        siSheWuRu(a);
    }


   public static void siSheWuRu(double a) {
        if (a * 10 % 10 >= 5) {
            int b = (int) a + 1;
            System.out.println("四舍五入后的结果为:" + b);
        } else {
            int b = (int) a;
            System.out.println("四舍五入后的结果为:" + b);
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小熊佩萁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值