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]
相关推荐
kill bert2 小时前
Java八股文背诵 第四天JVM
java·开发语言·jvm
√尖尖角↑3 小时前
力扣——【1991. 找到数组的中间位置】
算法·蓝桥杯
Allen Wurlitzer3 小时前
算法刷题记录——LeetCode篇(1.8) [第71~80题](持续更新)
算法·leetcode·职场和发展
百锦再5 小时前
五种常用的web加密算法
前端·算法·前端框架·web·加密·机密
你是理想6 小时前
wait 和notify ,notifyAll,sleep
java·开发语言·jvm
碳基学AI6 小时前
北京大学DeepSeek内部研讨系列:AI在新媒体运营中的应用与挑战|122页PPT下载方法
大数据·人工智能·python·算法·ai·新媒体运营·产品运营
helloworld工程师6 小时前
【微服务】SpringBoot整合LangChain4j 操作AI大模型实战详解
java·eclipse·tomcat·maven
Java&Develop6 小时前
idea里面不能运行 node 命令 cmd 里面可以运行咋回事啊
java·ide·intellij-idea
q567315236 小时前
使用Java的HttpClient实现文件下载器
java·开发语言·爬虫·scrapy
独家回忆3646 小时前
每日算法-250410
算法