Java练习九答案

求和

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

import java.util.Scanner;

/**
 * @title:Sueforpeace
 * @author:nanzhou
 * @date:2023.8.11
 */
//## 求和 设计一个方法,用于计算整数的和
public class Sueforpeace {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println(sueForpeace(1,2,3));
    }

    public static int sueForpeace(int... num) {
        int sum = num[0];
        for (int i = 0; i < num.length; i++) {
            sum += num[i];
        }
        return sum;
    }
}

阶乘

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

/**
 * @title:Factorial
 * @author:nanzhou
 * @date:2023.08.11
 */
//## 阶乘 编写一个方法,接受一个正整数作为参数,并返回它的阶乘
public class Factorial {
    public static  int factorial(int num){
        int nums =1;
        for (int i = num; i >0 ; i--) {
            nums *=i;
        }
        return nums;
    }
    public static void main(String[] args) {
        System.out.println(factorial(3));
    }
}

判断素数

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

import java.util.Scanner;

/**
 * @title:Prime
 * @author:nanzhou
 * @date:2023.8.11
 */
public class Prime {
    public static boolean prime(int num) {
        if (num == 0) {
            return false;
        } else {
            for (int i = 2; i < num; i++) {
                if (num % i == 0) {
                    return false;
                }
            }
            return true;
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println(prime(input.nextInt()));
    }
}

翻转数组

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

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * @title:Flip
 * @author:nanzhou
 * @date:2023.8.11
 */
public class Flip {
    public static char[] flipChar(char[] arr) {
        char[] newarr = new char[arr.length];
        for (int i = arr.length - 1; i >= 0; i--) {
            newarr[i] = arr[i];
        }
        System.out.println(Arrays.toString(newarr));
        return newarr;
    }

    public static void main(String[] args) {
        char[] arr = {'1', '2', '3'};
        arr = flipChar(arr);
    }
}

回文数

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

import java.util.Scanner;

/**
 * @title:Palindromic
 * @author:nanzhou
 * @date:2023.8.11
 */
//## 回文数
//        编写一个方法,接受一个整数作为参数,判断它是否为回文数(正着读和倒着读都一样)
public class Palindromic {
    public static boolean palindRomic(int num) {
        if (num < 0) {
            return false;
        } else {
            int temp = 0;
            while (num > temp) {
                temp = temp * 10 + num % 10;
                num /= 10;
            }
            return num == temp | num == temp / 10;
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println(palindRomic(input.nextInt()));
    }
}

交换元素位置

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

import java.util.Arrays;

/**
 * @title:Swap
 * @author:nanzhou
 * @date:2023.8.11
 */
//## 交换元素位置
//        编写一个方法,接受一个整数数组和两个索引作为参数,交换数组中指定索引位置的两个元素。
public class Swap {
    public static int[] swa(int[] arr, int m, int n) {
        arr[m] += arr[n];
        arr[n] = arr[m] - arr[n];
        arr[m] -= arr[n];
        return arr;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6};
        System.out.println(Arrays.toString(swa(arr, 2, 5)));
    }
}

数组元素左移

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

import java.util.Arrays;

/**
 * @title:
 * @author:nanzhou
 * @date:
 */
//## 数组元素左移
//        编写一个方法,接受一个整数数组和一个正整数 k 作为参数,将数组中的元素左移 k 个位置。
public class Moveleft {
    public static int[] moveLeft(int[] arr,int k){
        int[] temparr = new int[arr.length];
        System.arraycopy(arr, k, temparr, 0, arr.length - k);
        System.arraycopy(arr, 0, temparr, arr.length - k, k);
        return temparr;
    }
    public static void main(String[] args) {
        int[] arr = {9, 8, 6, 5, 7, 1, 2, 3};
        int k = 5;
        System.out.println(Arrays.toString(moveLeft(arr,k)));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

言行不易

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

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

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

打赏作者

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

抵扣说明:

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

余额充值