【Java 百“练”成钢】Java 基础:带参数的方法

01.求和

public class SumCalculator {
    static int calculateSum(int... numbers) {
        int sum = 0;
        for (int num : numbers) {
            sum += num;
        }
        return sum;
    }

    public static void main(String[] args) {
        int result = calculateSum(1, 2, 3, 4, 5);
        System.out.println("Sum : " + result);
    }
}

image.png

02.字符串输出

public class DisplayStrings {
    static void display(String... strings) {
        for (String str : strings) {
            System.out.println(str);
        }
    }

    public static void main(String[] args) {
        display("Blue", "Pink", "White", "Black", "Yellow");
    }
}

image.png

03.寻找最大值

public class MaxFinder {
    static int findMax(int... numbers) {
        if (numbers.length == 0) {
            throw new IllegalArgumentException("No numbers provided");
        }
        int max = numbers[0];
        for (int num : numbers) {
            if (num > max) {
                max = num;
            }
        }
        return max;
    }

    public static void main(String[] args) {
        int max = findMax(10, 86, 65, 5, 20, 15);
        System.out.println("Maximum Number is " + max);
    }
}

image.png

04.寻找最小值

public class MinFinder {
    static int findMin(int... numbers) {
        if (numbers.length == 0) {
            throw new IllegalArgumentException("No numbers provided");
        }
        int min = numbers[0];
        for (int num : numbers) {
            if (num < min) {
                min = num;
            }
        }
        return min;
    }

    public static void main(String[] args) {
        int min = findMin(80, 73, 65, 15, 39, 51);
        System.out.println("Minimum Number is " + min);
    }
}

image.png

05.字符串拼接

public class StringConcatenator {
    static String concatenateStrings(String... strings) {
        StringBuilder result = new StringBuilder();
        for (String str : strings) {
            result.append(str);
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String concatenatedString = concatenateStrings("Welcome, ", "Tutor ", "Joe's!");
        System.out.println("Concatenated String : " + concatenatedString);
    }
}

image.png

06.求平均值

public class AverageCalculator {
    static double calculateAverage(double... numbers) {
        if (numbers.length == 0) {
            throw new IllegalArgumentException("No numbers provided");
        }
        double sum = 0;
        for (double num : numbers) {
            sum += num;
        }
        return sum / numbers.length;
    }

    public static void main(String[] args) {
        double average = calculateAverage(10.5, 5.3, 7.8, 12.1);
        System.out.println("Average: " + average);
    }
}

image.png

07.数组排序

import java.util.Arrays;

public class SortNumbers {
    static void sortNumbers(int... numbers) {
        System.out.println("Given Numbers : " + Arrays.toString(numbers));
        Arrays.sort(numbers);
        System.out.println("Sorted Numbers : " + Arrays.toString(numbers));
    }

    public static void main(String[] args) {
        sortNumbers(80, 73, 65, 15, 39, 51);
    }
}

image.png

08.累乘

import java.util.Arrays;

public class ProductCalculator {
    static int calculateProduct(int... numbers) {
        System.out.println("Given Numbers : " + Arrays.toString(numbers));
        if (numbers.length == 0) {
            throw new IllegalArgumentException("No numbers provided");
        }
        int product = 1;
        for (int num : numbers) {
            product *= num;
        }
        return product;
    }

    public static void main(String[] args) {
        int product = calculateProduct(1, 2, 3, 4, 5);
        System.out.println("Product : " + product);
    }
}

image.png

09.存在的字符串

public class StringChecker {
    static boolean checkString(String search, String... strings) {
        for (String str : strings) {
            if (str.equals(search)) {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        boolean exists = checkString("Pink", "Blue", "White", "Pink", "Black");
        System.out.println("String Exists : " + exists);
    }
}

image.png

10.长整型求和

public class LongTotalCalculator {
    static long calculateTotal(long... numbers) {
        if (numbers.length == 0) {
            throw new IllegalArgumentException("No numbers provided");
        }
        long total = 0;
        for (long num : numbers) {
            total += num;
        }
        return total;
    }

    public static void main(String[] args) {
        long total = calculateTotal(1000000L, 2000000L, 3000000L);
        System.out.println("Total : " + total);
    }
}

image.png

11.寻找字符串索引

public class StringIndexFinder {
    static int findStringIndex(String search, String... strings) {
        for (int i = 0; i < strings.length; i++) {
            if (strings[i].equals(search)) {
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int index = findStringIndex("Pink", "Blue", "Pink", "White", "Black");
        if (index != -1) {
            System.out.println("String Found at index : " + index);
        } else {
            System.out.println("String Not Found");
        }
    }
}

image.png

12.字符串拼接(StringBuilder)

public class StringAppender {
    static String appendString(String base, String... strings) {
        StringBuilder result = new StringBuilder(base);
        for (String str : strings) {
            result.append(str);
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String finalString = appendString("Hello, ", "Java ", "Programmers ", "World!");
        System.out.println("Concatenated String : " + finalString);
    }
}

image.png

  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

G皮T

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

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

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

打赏作者

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

抵扣说明:

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

余额充值