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]
相关推荐
键盘鼓手苏苏2 小时前
Flutter for OpenHarmony:markdown 纯 Dart 解析引擎(将文本转化为结构化 HTML/UI) 深度解析与鸿蒙适配指南
前端·网络·算法·flutter·ui·html·harmonyos
郝学胜-神的一滴3 小时前
当AI遇见架构:Vibe Coding时代的设计模式复兴
开发语言·数据结构·人工智能·算法·设计模式·架构
Frostnova丶8 小时前
LeetCode 190.颠倒二进制位
java·算法·leetcode
骇城迷影9 小时前
代码随想录:链表篇
数据结构·算法·链表
闻哥9 小时前
Redis事务详解
java·数据库·spring boot·redis·缓存·面试
hrhcode9 小时前
【Netty】五.ByteBuf内存管理深度剖析
java·后端·spring·springboot·netty
道亦无名9 小时前
aiPbMgrSendAck
java·网络·数据库
专注前端30年9 小时前
智能物流路径规划系统:核心算法实战详解
算法
发现你走远了10 小时前
Windows 下手动安装java JDK 21 并配置环境变量(详细记录)
java·开发语言·windows
心 -10 小时前
java八股文DI
java