原标题:Java语言基础语法部分编程题(11-20)

public class Test11 {public static void main(String[] args) {// (11) 考点:数组int[] arr = {5,2,7,1,4};// 从前向后遍历打印所有元素
for (int i = 0; i = 0 ; i--) {
System.out.println(arr[i]);}}}public class Test12 {public static void main(String[] args) {// (12) 考点:数组
int[] arr = {7,3,5,6,2};// 求数组中所有元素之和?int x = 0;for (int i = 0; i < arr.length; i++) {x += arr[i];}System.out.println(x);
// 求数组中所有偶数之和?int y = 0;for (int i = 0; i < arr.length; i++) {if (arr[i] % 2 == 0) {y += arr[i];}}System.out.println(y);
// 求数组中有多少个偶数?int z = 0;for (int i = 0; i < arr.length; i++) {if (arr[i] % 2 == 0) {z++;}}System.out.println(z);
}}public class Test13 {public static void main(String[] args) {// (13)求数组元素平均值int[] arr = {1,2,3,4,5};
}}public class Test14 {public static void main(String[] args) {// (14)求数组中最大的元素int[] arr = {5, 3, 8, 2, 6};
}}public class Test15 {public static void main(String[] args) {// (15)定义实现m方法:返回2个数中的最大值// int max = m(2, 3);
// System.out.println(max);}}public class Test16 {public static void main(String[] args) {// (16)定义实现m方法:返回3个数中的最大值
int max = m(2, 3, 5);System.out.println(max);}public static int m(int a, int b, int c) {int max = 0;if (a > b && a > c) {
max = a;} else if (b > a && b > c) {max = b;} else {max = c;}return max;}}public class Test17 {public static void main(String[] args) {
// (17)定义实现m方法:求数组中元素和// int[] arr = {1,2,3,4,5};// int sum = m(arr);// System.out.println(sum);}}public class Test18 {
public static void main(String[] args) {// (18)定义实现m方法:求数组中最大值// int[] arr = {1,2,3,4,5};// int max = m(arr);
// System.out.println(max);}}public class Test19 {public static void main(String[] args) {// (19)定义实现m方法:求数组中最小值的偶数
// int[] arr = {1,2,3,4,5};// int min = m(arr);// System.out.println(min);}}public class Test20 {public static void main(String[] args) {
// (20)定义实现m方法:在arr中找出指定元素的下标、如果没有元素、则返回-1int a = 2;int[] arr = {1,2,3,4,5};int b = m(arr, a);System.out.println(b);
}public static int m(int[] x, int b) {int y = -1;for (int i = 0; i < x.length; i++) {if (x[i] == y) {
y = i;break;}}return y;}public static int m2(int[] x, int b) {for (int i = 0; i < x.length; i++) {if (x[i] == b) {
return i;}}return -1;}}返回搜狐,查看更多责任编辑:
亲爱的读者们,感谢您花时间阅读本文。如果您对本文有任何疑问或建议,请随时联系我。我非常乐意与您交流。
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。