int[] arr = {1,3,3,2,8};
String str="";
int i = 0;
public void doIt(){
for(;i<arr.length;){
if(arr[i]>arr[i+1]){
str=arr[i]+arr[i+1]+str;
break;
}else if(arr[i]<arr[i+1]){
str=arr[i]+strarr[i+1];
i++;
continue;
}else{
str+=arr[i];
i++;
}
}
System.out.println(i);
System.our.println(str);
String str="";
int i = 0;
public void doIt(){
for(;i<arr.length;){
if(arr[i]>arr[i+1]){
str=arr[i]+arr[i+1]+str;
break;
}else if(arr[i]<arr[i+1]){
str=arr[i]+strarr[i+1];
i++;
continue;
}else{
str+=arr[i];
i++;
}
}
System.out.println(i);
System.our.println(str);
第一次1<3 所以str=arr[0]+str+arr[1]; str="13"
第二次3=3 所以 str+=arr[i];str="133"
第三次3>2 所以 str=arr[2]+arr[3]+str;str="32133" 这时候break所以i=2 str="32133" 一步步循环就是
这句你写错了,改正过来,我给你运行结果。
public class Test13 { /**
* @param args
*/
public static void main(String[] args) {
int[] arr = {1,3,3,2,8};
String str="";
int i = 0;
for(;i<arr.length;){
if(arr[i]>arr[i+1]){
str=arr[i]+arr[i+1]+str;
break;
}else if(arr[i]<arr[i+1]){
str=arr[i]+str+arr[i+1];
i++;
continue;
}else{
str+=arr[i];
i++;
}
System.out.println(i);
System.out.println(str); }
}}输出:2
123
输出结果:
2
5133package qustion;public class DoIt {
int[] arr = { 1, 3, 3, 2, 8 };
String str = "";
int i = 0; public void doIt() {
for (; i < arr.length;) {
if (arr[i] > arr[i + 1]) {
str = arr[i] + arr[i + 1] + str;
break;
} else if (arr[i] < arr[i + 1]) {
str = arr[i] + str + arr[i + 1];
i++;
continue;
} else {
str += arr[i];
i++;
}
}
System.out.println(i);
System.out.println(str);
} public static void main(String[] args) {
DoIt d = new DoIt();
d.doIt();
}
}
int[] arr = { 1, 3, 3, 2, 8 };
i = 0, a[i] < a[i+1]: str = 1+""+3 = 13, continue;
i = 1, a[i] = a[i+1]: str = 13 + a[i] = 133,continue;
i = 2, a[i] > a[i+1]: str = 3+2+str = 5133,break.
这里两个int型在一起先相加在转换