JAVA基础(代码)练习题61~90
JAVA基础
61.设计一个方法打印数组{‘a’,‘r’,‘g’,‘s’,‘e’,‘r’}中下标为1和3的的元素
package Homework_90; /** * 设计一个方法打印数组{‘a’,‘r’,‘g’,‘s’,‘e’,‘r’}中下标为1和3的的元素 * @author Administrator * */ public class Homework61 { public static void main(String[] args) { char[] a = {'a', 'r', 'g', 's', 'e', 'r'}; printArray(a); } public static void printArray(char[] a) { System.out.print(a[1] + " "); System.out.print(a[3]); } }
View Code
62.设计一个方法打印数组{1,5,4,8,79,1,6}中下标为2和3的的元素
package Homework_90; /** * 设计一个方法打印数组{1,5,4,8,79,1,6}中下标为2和3的的元素 * @author Administrator * */ public class Homework62 { public static void main(String[] args) { int[] arr = {1,5,4,8,79,1,6}; printArray(arr); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i == 2 || i == 3) { System.out.println(arr[i]); } } } }
View Code
63.设计一个方法打印数组{2,5,66,12,66,7,63,321}中下标为4和6的的元素
package Homework_90; /** * 设计一个方法打印数组{2,5,66,12,66,7,63,321}中下标为4和6的的元素 * @author Administrator * */ public class Homework63 { public static void main(String[] args) { int[] arr = {2,5,66,12,66,7,63,321}; printArray(arr); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i == 4 || i == 6) { System.out.println(arr[i]); } } } }
View Code
64.设计一个方法打印数组{5,48,68,324,4821,457,351,354} 中下标为2和5的的元素
package Homework_90; /** * 设计一个方法打印数组{5,48,68,324,4821,457,351,354} 中下标为2和5的的元素 * @author Administrator * */ public class Homework64 { public static void main(String[] args) { int[] arr = {5,48,68,324,4821,457,351,354}; printArray(arr); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i == 2 || i == 5) { System.out.println(arr[i]); } } } }
View Code
65.设计一个方法打印数组{“我”,“是”,“一个”,“吃”,“货”,“啊”}中下标为0和2的的元素
package Homework_90; /** * 设计一个方法打印数组{“我”,“是”,“一个”,“吃”,“货”,“啊”}中下标为0和2的的元素 * @author Administrator * */ public class Homework65 { public static void main(String[] args) { String[] arr = {"我","是","一个","吃","货","啊"}; printArray(arr); } public static void printArray(String[] arr) { for (int i = 0; i < arr.length; i++) { if (i == 0 || i == 2) { System.out.println(arr[i]); } } } }
View Code
66.设计一个方法打印数组 {1,2,3,6,5,4,7}中值为2和4的下标
package Homework_90; /** * 设计一个方法打印数组 {1,2,3,6,5,4,7}中值为2和4的下标 * @author Administrator * */ public class Homework66 { public static void main(String[] args) { int[] arr = {1,2,3,6,5,4,7}; printArray(arr); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] == 2 || arr[i] == 4) { System.out.println(i); } } } }
View Code
67.设计一个方法打印数组 {5,4,8,7,9,6}中值为8和6的下标
package Homework_90; /** * 设计一个方法打印数组 {5,4,8,7,9,6}中值为8和6的下标 * @author Administrator * */ public class Homework67 { public static void main(String[] args) { int[] arr = {5,4,8,7,9,6}; printArray(arr); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] == 8 || arr[i] == 6) { System.out.println(i); } } } }
View Code
68.设计一个方法打印数组 {4,55,99,77,7,5,2}中值为55和7的下标
package Homework_90; /** * 设计一个方法打印数组 {4,55,99,77,7,5,2}中值为55和7的下标 * @author Administrator * */ public class Homework68 { public static void main(String[] args) { int[] arr = {4,55,99,77,7,5,2}; printArray(arr); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] == 55 || arr[i] == 7) { System.out.println(i); } } } }
View Code
69.设计一个方法打印数组 {‘a’,’d’,’d’,’s’,’f’,’g’}中值为a和g的下标
package Homework_90; /** * 设计一个方法打印数组 {‘a’,’d’,’d’,’s’,’f’,’g’}中值为a和g的下标 * @author Administrator * */ public class Homework69 { public static void main(String[] args) { char[] arr = {'a','d','d','s','f','g'}; printArray(arr); } public static void printArray(char[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] == 'a' || arr[i] == 'g') { System.out.println(i); } } } }
View Code
70.设计一个方法打印数组 {5.2 ,4.5 ,8.8 ,7.8 ,9.1 ,6.0}中值为5.2和7.8的下标
package Homework_90; /** * 设计一个方法打印数组 {5.2 ,4.5 ,8.8 ,7.8 ,9.1 ,6.0}中值为5.2和7.8的下标 * @author Administrator * */ public class Homework70 { public static void main(String[] args) { double[] arr = {5.2, 4.5, 8.8, 7.8, 9.1, 6.0}; printArray(arr); } public static void printArray(double[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] == 5.2 || arr[i] == 7.8) { System.out.println(i); } } } }
View Code
71.设计一个方法打印数组 {“狗”,“哇”,“兔兔”,“鱼”,“牛”,“猫”}中值为兔兔和鱼的下标
package Homework_90; /** * 设计一个方法打印数组 {“狗”,“哇”,“兔兔”,“鱼”,“牛”,“猫”}中值为兔兔和鱼的下标 * @author Administrator * */ public class Homework71 { public static void main(String[] args) { String[] arr = {"狗", "哇", "兔兔", "鱼", "牛", "猫"}; printArray(arr); } public static void printArray(String[] arr) { for (int i = 0; i < arr.length; i++) { if (arr[i] == "兔兔" || arr[i] == "鱼") { System.out.println(i); } } } }
View Code
72.设计一个方法求出数组{1,3,4,5,62,3,6}中第一个元素和第三个元素的和
package Homework_90; /** * 设计一个方法求出数组{1,3,4,5,62,3,6}中第一个元素和第三个元素的和 * @author Administrator * */ public class Homework72 { public static void main(String[] args) { int[] arr = {1, 3, 4, 5, 62, 3, 6}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i <= arr.length; i++) { if (i == 0 || i == 2) { sum = arr[i] + sum; } } return sum; } }
View Code
73.设计一个方法求出数组{1,3,75,33,62,78,99}所有元素的和
package Homework_90; /** * 设计一个方法求出数组{1,3,75,33,62,78,99}所有元素的和 * @author Administrator * */ public class Homework73 { public static void main(String[] args) { int[] arr = {1, 3, 75, 33, 62, 78, 99}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { sum = arr[i] + sum; } return sum; } }
View Code
74.设计一个方法求出数组{2,33,6,52,2,5,12,5}所有元素的和
package Homework_90; /** * .设计一个方法求出数组{2,33,6,52,2,5,12,5}所有元素的和 * @author Administrator * */ public class Homework74 { public static void main(String[] args) { int[] arr = {2, 33, 6, 52, 2, 5, 12, 5}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { sum = arr[i] + sum; } return sum; } }
View Code
75.设计一个方法求出数组{2,3,4,86,9,20,44,5}的奇数和
package Homework_90; /** * .设计一个方法求出数组{2,3,4,86,9,20,44,5}的奇数和 * @author Administrator * */ public class Homework75 { public static void main(String[] args) { int[] arr = {2, 3, 4, 86, 9, 20, 44, 5}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 != 0) { sum = arr[i] + sum; } } return sum; } }
View Code
76.设计一个方法求出数组{12,11,123,56,7,8,12,5}的偶数和
package Homework_90; /** * 设计一个方法求出数组{12,11,123,56,7,8,12,5}的偶数和 * @author Administrator * */ public class Homework76 { public static void main(String[] args) { int[] arr ={ 12, 11, 123, 56, 7, 8, 12, 5}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] % 2 == 0) { sum = arr[i] + sum; } } return sum; } }
View Code
77.设计一个方法求出数组{12,11,123,56,7,8,12,5}的下标为偶数的元素和
package Homework_90; /** * 设计一个方法求出数组{12,11,123,56,7,8,12,5}的下标为偶数的元素和 * @author Administrator * */ public class Homework77 { public static void main(String[] args) { int[] arr ={ 12, 11, 123, 56, 7, 8, 12, 5}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { if (i % 2 == 0) { sum = arr[i] + sum; } } return sum; } }
View Code
78.设计一个方法求出数组{12,11,123,56,7,8,12,5}的下标为奇数的元素和
package Homework_90; /** * 设计一个方法求出数组{12,11,123,56,7,8,12,5}的下标为奇数的元素和 * @author Administrator * */ public class Homework78 { public static void main(String[] args) { int[] arr ={ 12, 11, 123, 56, 7, 8, 12, 5}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { if (i % 2 != 0) { sum = arr[i] + sum; } } return sum; } }
View Code
79.设计一个方法求出数组{12,11,123,56,7,8,12,5}的下标为是3的倍数的元素的和
package Homework_90; /** * 设计一个方法求出数组{12,11,123,56,7,8,12,5}的下标为是3的倍数的元素的和 * @author Administrator * */ public class Homework79 { public static void main(String[] args) { int[] arr ={ 12, 11, 123, 56, 7, 8, 12, 5}; int sum = getSum(arr); System.out.println(sum); } public static int getSum(int[] arr) { int sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { if (i % 3 == 0) { sum = arr[i] + sum; } } return sum; } }
View Code
80.设计一个方法求出数组{12,11,123,56,7,8,12,5}的平均数
package Homework_90; /** * 设计一个方法求出数组{12,11,123,56,7,8,12,5}的平均数 * @author Administrator * */ public class Homework80 { public static void main(String[] args) { int[] arr ={ 12, 11, 123, 56, 7, 8, 12, 5}; double sum = getSum(arr); System.out.println(sum); } public static double getSum(int[] arr) { double sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { sum = arr[i] + sum; } sum = sum / arr.length; return sum; } }
View Code
81.设计一个方法求出数组{1,3,75,33,62,78,99}的平均数
package Homework_90; /** * 设计一个方法求出数组{1,3,75,33,62,78,99}的平均数 * @author Administrator * */ public class Homework81 { public static void main(String[] args) { int[] arr = {1, 3, 75, 33, 62, 78, 99}; double sum = getSum(arr); System.out.println(sum); } public static double getSum(int[] arr) { double sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { sum = arr[i] + sum; } sum = sum / arr.length; return sum; } }
View Code
82.设计一个方法求出数组{2,33,6,52,2,5,12,5}的平均数
package Homework_90; /** * 设计一个方法求出数组{2,33,6,52,2,5,12,5}的平均数 * @author Administrator * */ public class Homework82 { public static void main(String[] args) { int[] arr = {2, 33, 6, 52, 2, 5, 12, 5}; double sum = getSum(arr); System.out.println(sum); } public static double getSum(int[] arr) { double sum = 0; int num1 = 0; int num2 = 0; for (int i = 0; i < arr.length; i++) { sum = arr[i] + sum; } sum = sum / arr.length; return sum; } }
View Code
84.设计一个方法求出数组{2,33,6,52,2,5,12,5}的最大数和最小数
package Homework_90; /** * 设计一个方法求出数组{2,33,6,52,2,5,12,5}的最大数和最小数 * @author Administrator * */ public class Homework84 { public static int max; public static int min; public static void main(String[] args) { int[] arr = {2, 33, 6, 52, 2, 5, 12, 5}; getMaxMin(arr); System.out.println(max); System.out.println(min); } public static void getMaxMin(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] < arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } max = arr[0]; min = arr[arr.length-1]; } }
View Code
85.设计一个方法求出数组{12,11,123,56,7,8,12,5}的最大数和最小数
package Homework_90; /** * 设计一个方法求出数组{12,11,123,56,7,8,12,5}的最大数和最小数 * @author Administrator * */ public class Homework85 { public static int max; public static int min; public static void main(String[] args) { int[] arr = {12, 11, 123, 56, 7, 8, 12, 5}; getMaxMin(arr); System.out.println(max); System.out.println(min); } public static void getMaxMin(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] < arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } max = arr[0]; min = arr[arr.length-1]; } }
View Code
86.设计一个方法求出数组{1,3,75,33,62,78,99}的最大数和最小数
package Homework_90; /** * 设计一个方法求出数组{1,3,75,33,62,78,99}的最大数和最小数 * @author Administrator * */ public class Homework86 { public static int max; public static int min; public static void main(String[] args) { int[] arr = {1, 3, 75, 33, 62, 78, 99}; getMaxMin(arr); System.out.println(max); System.out.println(min); } public static void getMaxMin(int[] arr) { for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] < arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } max = arr[0]; min = arr[arr.length-1]; } }
View Code
87.设计一个方法求出任意int数组的所有元素的和
package Homework_90; import java.util.Scanner; /** * 设计一个方法求出任意int数组的所有元素的和 * @author Administrator * */ public class Homework87 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入数据以逗号隔开:"); String str = scan.next().toString(); // 输入字符串 String[] arr = str.split(","); // 以逗号分隔开 int[] b = new int[arr.length]; // 创建int类型数组,将接受的string类型传入 for (int i = 0; i < b.length; i++) { b[i] = Integer.parseInt(arr[i]); // 类型转换 } int sum = getArraySum(b); System.out.println(sum); } public static int getArraySum(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) { sum = sum + arr[i]; } return sum; } }
View Code
88.设计一个方法求出任意int数组中最大的值
package Homework_90; import java.util.Scanner; /** * 设计一个方法求出任意int数组中最大的值 * @author Administrator * */ public class Homework88 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入数据以逗号隔开:"); String str = scan.next().toString(); // 输入字符串 String[] arr = str.split(","); // 以逗号分隔开 int[] b = new int[arr.length]; // 创建int类型数组,将接受的string类型传入 for (int i = 0; i < b.length; i++) { b[i] = Integer.parseInt(arr[i]); // 类型转换 } int sum = getArraySum(b); System.out.println(sum); } public static int getArraySum(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] < arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } sum = arr[0]; return sum; } }
View Code
89.设计一个方法求出任意int数组中最小的值
package Homework_90; import java.util.Scanner; /** * 设计一个方法求出任意int数组中最小的值 * @author Administrator * */ public class Homework89 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入数据以逗号隔开:"); String str = scan.next().toString(); // 输入字符串 String[] arr = str.split(","); // 以逗号分隔开 int[] b = new int[arr.length]; // 创建int类型数组,将接受的string类型传入 for (int i = 0; i < b.length; i++) { b[i] = Integer.parseInt(arr[i]); // 类型转换 } int sum = getArraySum(b); System.out.println(sum); } public static int getArraySum(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] < arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } sum = arr[arr.length-1]; return sum; } }
View Code
90.设计一个方法求出任意int数组中第二大的值
package Homework_90; import java.util.Scanner; /** * 设计一个方法求出任意int数组中第二大的值 * @author Administrator * */ public class Homework90 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入数据以逗号隔开:"); String str = scan.next().toString(); // 输入字符串 String[] arr = str.split(","); // 以逗号分隔开 int[] b = new int[arr.length]; // 创建int类型数组,将接受的string类型传入 for (int i = 0; i < b.length; i++) { b[i] = Integer.parseInt(arr[i]); // 类型转换 } int sum = getArraySum(b); System.out.println(sum); } public static int getArraySum(int[] arr) { int sum = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length - i - 1; j++) { if (arr[j] < arr[j+1]) { int temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } sum = arr[1]; return sum; } }
View Code