Java基础练习九(方法)

求和

设计一个方法,用于计算整数的和

java 复制代码
public class Work1101 {
    public static void main(String[] args) {
        // 设计一个方法,用于计算整数的和
        System.out.println(sum(7, 6));
    }
    public static int sum(int a, int b) {
        return a + b;
    }
}

阶乘

编写一个方法,接受一个正整数作为参数,并返回它的阶乘

java 复制代码
import java.util.Scanner;

public class Factorial1102 {
    public static int factorial(int a) {
        int product = 1;
        for (int j = 1; j <= a; j++) {
            product *= j;
        }
        return product;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int input = sc.nextInt();
        int result = factorial(input);
        System.out.println(input + " 的阶乘为:" + result);
    }
}

判断素数

编写一个方法,接受一个整数作为参数,并判断它是否为素数(质数)

java 复制代码
import java.util.Scanner;

public class IsPrimeNumber1103 {
    public static boolean isPrimeNumber(int n) {
        boolean flag = true;
        for (int i = 2; i < n; i++) {
            if (n % i == 0) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个数");
        int num = sc.nextInt();
        System.out.println(num + (isPrimeNumber(num) ? "是素数" : "不是素数"));
    }
}

翻转数组

编写一个方法,接受一个字符数组,将其翻转并输出。

java 复制代码
public class ArrayReverse {
    public static void reverseAndPrint(char[] arr) {
        int left = 0;
        int right = arr.length - 1;

        while (left < right) {
            char temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
            left++;
            right--;
        }

        System.out.println("翻转后的数组为:");
        for (char c : arr) {
            System.out.print(c);
        }
        System.out.println();
    }

    public static void main(String[] args) {
        char[] chars = {'H', 'e', 'l', 'l', 'o'};
        reverseAndPrint(chars);
    }
}

回文数

编写一个方法,接受一个整数作为参数,判断它是否为回文数(正着读和倒着读都一样)

java 复制代码
public class Palindrome1105 {
    public static boolean isPalindrome(int num) {
        String numStr = Integer.toString(num);
        int right = numStr.length() - 1;
        int left = 0;
        while (left < right) {
            if (numStr.charAt(left) != numStr.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
    public static void main(String[] args) {
        System.out.println(isPalindrome(1232));
    }
}

交换元素位置

编写一个方法,接受一个整数数组和两个索引作为参数,交换数组中指定索引位置的两个元素。

java 复制代码
import java.util.Arrays;

public class Work1106 {
    public static void main(String[] args) {
        // 交换元素位置
        int[] arr = {1, 2, 3, 4, 5, 6, 7};
        PositionExchange(arr,1,5);
        System.out.println(Arrays.toString(arr));
    }
    public static int[] PositionExchange(int[] arr, int begin, int end) {
        if (begin >=0 && begin < arr.length && end >=0 && end <arr.length) {
            int temp = arr[begin];
            arr[begin] = arr[end];
            arr[end] = temp;

            return arr;
        } else {
            return null;
        }
    }
}

数组元素左移

编写一个方法,接受一个整数数组和一个正整数 k 作为参数,将数组中的元素左移 k 个位置。

java 复制代码
import java.util.Arrays;

public class LeftShift1107 {
    public static void leftshift(int[] arr, int k) {
        int[] arr1 = Arrays.copyOf(arr,arr.length);
        for (int i = 0; i < arr.length; i++) {
            if (i - k >= 0) {
                arr[i - k] = arr1[i];
            } else {
                arr[arr.length - k + i] = arr1[i];
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        leftshift(arr,3);
        System.out.println(Arrays.toString(arr));
    }
}
// 输出:[4, 5, 1, 2, 3]
相关推荐
独自破碎E几秒前
BISHI75 阶幂
android·java·开发语言
红中️几秒前
Tomcat
java·tomcat
田里的水稻1 分钟前
OE_ubuntu24.04安装ros2
人工智能·算法·数学建模·机器人·自动驾驶
Charlie_lll3 分钟前
力扣解题-无重复字符的最长子串
后端·算法·leetcode
爱学习的小可爱卢4 分钟前
JavaSE基础-Java异常体系:Bug定位终极指南
java·bug·javase
W133309089075 分钟前
大专应用统计学专业,怎么区分数据统计岗和数据分析岗?
人工智能·算法·数据分析
甲枫叶6 分钟前
【claude+weelinking产品经理系列15】UI/UX 打磨——产品经理的审美终于能自己实现
java·人工智能·python·ui·产品经理·ai编程·ux
羑悻的小杀马特6 分钟前
LFU缓存算法全解:从双哈希+双向链表到O(1)艺术,解锁长期热点守护神
算法·缓存·哈希算法·lfu·双链表
kebijuelun7 分钟前
GLM-5:从 Vibe Coding 走向 Agentic Engineering 的全栈路线图
人工智能·深度学习·算法·语言模型
zihan03218 分钟前
将若依(RuoYi)框架从适配 Spring Boot 2 的版本升级到 Spring Boot 3
java·spring boot·github·若依框架