1.问题是:
在数组{4,5,6,2,3,1,9,8,7,10,12,14,15}中查找元素(先打印输出所有元素,输入一个数,如果找到了则打印输出其位置,没有找到则提示没有找到)
public static void main(String[] args) {
int[] a = {4, 5, 6, 2, 3, 1, 9, 8, 7, 10, 12, 14, 15};
System.out.print("所有元素: ");
for (int i : a) {
System.out.print(i + " ");
}
int b = 14;
boolean found = false;
for (int i = 0; i < a.length; i++) {
if (a[i] == b) {
System.out.println("找到了,位置是"+i);
found = true;
break;
}
}
if (!found) {
System.out.println("没有找到");
}
}
2.问题是:
(1)定义数字字符串数组"“010",3223","666","7890987","123123"}
(2)判断该数字字符串数组中的数字字符串是否是对称(第一个数字和最后一个数字相等,第二个数字和倒数第二个数字是相等的,依次类推)的,并逐个输出
public static void main(String[] args) {
String s[] ={"010","3223","666","7890987","123123"};
for (int i = 0; i <s.length ; i++) {
for (int j = 0; j <s[i].length()/2 ; j++) {
if (s[i].charAt(j)==s[i].charAt(s[i].length()-i-1)){
if (j==s[i].length()/2-1){
System.out.println(s[i]+"是对称的");
}
}else{
System.out.println(s[i]+"是不对称的");
break;
}
}
}
}