java简单语法(Java语言基础语法部分编程题(21-32))

feifei123 发布于 2025-02-24 阅读(20)

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

public class Test21 {public static void main(String[] args) {// (21)找出数中最大元素的下标int[] arr = {5, 3, 7, 2, 6};

int x = 0;for (int i = 0; i arr[x]) {x = i;}}System.out.println(x);}

}public class Test22 {public static void main(String[] args) {// (22)元素3和元素2互换位置int[] arr = {5, 3, 7, 2, 6};

int a = arr[1];arr[1] = arr[3];arr[3] = a;for (int i = 0; i < arr.length; i++) {System.out.println(arr[i]);

}}}public class Test23 {public static void main(String[] args) {// (23)定义实现m方法:判断数组中是否有指定的元素,如果有返回true、否则返回false

int[] arr = {1,2,3,4,5};boolean a = m(arr, 3);System.out.println(a);}public static boolean m(int[] arr, int a) {

for (int i = 0; i < arr.length; i++) {if (arr[i] == a) {return true;}}return false;}}public class Test24 {

public static void main(String[] args) {// 说明:先掌握以下2个语句区别、再做后边的题// 不换行打印语句代码如下:System.out.print(1);System.out.print(2);

System.out.print(3);// 换行打印语句代码如下:System.out.println();System.out.println(4);System.out.println(5);System.out.println(6);

}}public class Test25 {public static void main(String[] args) {// (25) 用*在控制台输出以下图形:长方形// *****// *****

// *****}}public class Test26 {public static void main(String[] args) {// (26) 用*在控制台输出以下图形// 正直角三角形

// *// **// ***// ****}}public class Test27 {public static void main(String[] args) {

// (27) 用*在控制台输出以下图形// 倒直角三角形// ****// ***// **// *}}public class Test28 {

public static void main(String[] args) {// (28) 用*在控制台输出以下图形// 打印如下数字矩阵// 1// 12// 123// 1234// 12345

// 打印如下数字矩阵// 1// 121// 12321// 1234321// 123454321}}public class Test29 {public static void main(String[] args) {

// (29)打印两个数组的交集// 循环嵌套!// 方法!int[] arr = {1,2,3,4,5};int[] brr = {3,4,5,6,7};for (int i = 0; i < arr.length; i++) {

for (int j = 0; j < brr.length; j++) {if (arr[i] == brr[j]) {System.out.println(arr[i]);}}}for (int i = 0; i < arr.length; i++) {

if (m(brr, arr[i])) {System.out.println(arr[i]);}}}public static boolean m(int[] brr, int a) {for (int i = 0; i < brr.length; i++) {

if (brr[i] == a) {return true;}}return false;}}public class Test30 {public static void main(String[] args) {

// (30)// (1)ABCDE=4*EDCBA;其中,A、B、C、D、E都是1-9中的数字问这5个数字都是多少//// (2)有一工人甲,工资是三位数ABC元(一个字母代表0-9中一个数字),。

// 组内其它五个工人的工资可以这样表示:ACB,BAC,BCA,CAB,CBA,且这五个工人的工资总额为3194元// 请问工人甲的工资具体是多少for (int abc = 100; abc <= 999; abc++) {。

int a = abc / 100;int b = abc / 10 % 10;int c = abc % 10;if ((a*100+c*10+b + b*100+a*10+c + b*100+c*10+a + c*100+a*10+b + c*100+b*10+a) == 3194 ) {

System.out.println(abc);}}//// (3)所谓水仙花数是指一个三位数,其各位数字立方和等于该数本身,// 例如153是水仙花数,因为153=1*1*1+5*5*5+3*3*3.请编程求出所有的水仙花数?

//// (4)一个数如果恰好等于它的因子之和,这个数就称为“完数”例如6=1+2+3.编程// 找出10000以内的所有完数}}public class Test31 {public static void main(String[] args) {。

// (31)找出数组中最大的偶数int[] arr = {3,4,5,6,7,8,9};int max = arr[0];for (int i = 0; i < arr.length; i++) {// if (arr[i] % 2 == 0) {

// if (arr[i] > max) {// max = arr[i];// }// }if ((arr[i] % 2 == 0) && (arr[i] > max)) {

max = arr[i];}}System.out.println(max);}}public class Test32 {public static void main(String[] args) {

// (32)打印出两个数组的差集// arr中有brr中没有的元素int[] arr = {1,2,3,4,5};int[] brr = {3,4,5,6,7};for (int i = 0; i < arr.length; i++) {

if (!m(brr, arr[i])) {System.out.println(arr[i]);}}}public static boolean m(int[] brr, int b) {for (int i = 0; i < brr.length; i++) {

if (brr[i] == b) {return true;}}return false;}}返回搜狐,查看更多责任编辑:

亲爱的读者们,感谢您花时间阅读本文。如果您对本文有任何疑问或建议,请随时联系我。我非常乐意与您交流。

标签:  控制台 数字 打印 水仙花 元素 

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。