publicclassSumCalculator{staticintcalculateSum(int... numbers){int sum =0;for(int num : numbers){
sum += num;}return sum;}publicstaticvoidmain(String[] args){int result =calculateSum(1,2,3,4,5);System.out.println("Sum : "+ result);}}
publicclassMaxFinder{staticintfindMax(int... numbers){if(numbers.length ==0){thrownewIllegalArgumentException("No numbers provided");}int max = numbers[0];for(int num : numbers){if(num > max){
max = num;}}return max;}publicstaticvoidmain(String[] args){int max =findMax(10,86,65,5,20,15);System.out.println("Maximum Number is "+ max);}}
04.寻找最小值
publicclassMinFinder{staticintfindMin(int... numbers){if(numbers.length ==0){thrownewIllegalArgumentException("No numbers provided");}int min = numbers[0];for(int num : numbers){if(num < min){
min = num;}}return min;}publicstaticvoidmain(String[] args){int min =findMin(80,73,65,15,39,51);System.out.println("Minimum Number is "+ min);}}
publicclassAverageCalculator{staticdoublecalculateAverage(double... numbers){if(numbers.length ==0){thrownewIllegalArgumentException("No numbers provided");}double sum =0;for(double num : numbers){
sum += num;}return sum / numbers.length;}publicstaticvoidmain(String[] args){double average =calculateAverage(10.5,5.3,7.8,12.1);System.out.println("Average: "+ average);}}
publicclassLongTotalCalculator{staticlongcalculateTotal(long... numbers){if(numbers.length ==0){thrownewIllegalArgumentException("No numbers provided");}long total =0;for(long num : numbers){
total += num;}return total;}publicstaticvoidmain(String[] args){long total =calculateTotal(1000000L,2000000L,3000000L);System.out.println("Total : "+ total);}}
11.寻找字符串索引
publicclassStringIndexFinder{staticintfindStringIndex(String search,String... strings){for(int i =0; i < strings.length; i++){if(strings[i].equals(search)){return i;}}return-1;}publicstaticvoidmain(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");}}}