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]
相关推荐
Sarvartha10 分钟前
Lambda 表达式和函数式接口
java·开发语言
计算机毕业设计杰瑞16 分钟前
【计算机毕设精品】基于SpringBoot的一体化教务与学业质量管理系统的设计与实现,附源码_增删改查_java项目_可完美运行部署_网站项目
java·spring boot·课程设计
colinu32 分钟前
C++ 异步任务与线程生命周期:`std::async`、`future`、`thread`、`jthread` 与协作式退出
java·jvm·c++
蜗牛互联网1 小时前
Python + JSON Schema 实现工单结构化输出与本地复核
java·人工智能·后端
逐光者9331 小时前
STM32——SPI 屏 · Flash · 高速
java·前端·stm32
动词ing1 小时前
【题目练习】算法 组合总和||
算法
wuminyu1 小时前
ForkJoinPool窃取线程在扫描其他WorkQueue时的随机采样算法与Phase线程挂起唤醒机制解析
java·linux·c语言·jvm·c++
longlongzihan1 小时前
链表找环:Floyd 判圈算法(LeetCode 141 & 142 )
c++·算法·leetcode·链表
Sarvartha1 小时前
内部类知识
java·开发语言
Knight_AL1 小时前
从 EasyExcel 到 Apache Fesod:Java Excel 处理生态的一次重构
java·apache·excel