网易练习

大家帮我看看,我应该怎么去改呀!
import java.util.Scanner;
public class Main {
public static void print(int[] a,int cnt){
int i,j,k,temp;
for(i = 0;i < cnt;i++){
k = i;
for(j = i+1;j < cnt;j++)
if(a[k] > a[j])
k = j;
if(k != j){
temp = a[k];
a[k] = a[i];
a[i] = temp;
}
}
int s = sum2(a) - sum1(a);
System.out.printf("%04d = %04d - %04d\n",s,sum2(a),sum1(a)); 
if(s != 6174){
test(s);
}else{
return;
}
}
public static void test(int s){
int[] b = new int[4];
int h = s;
int n = 0;
while(h != 0){
b[n] = h % 10;
h /= 10;
n++;
}
print(b,4);
}
public static int sum2(int[] a){
int count = 0;
int start = 1000;
for(int i = a.length-1;i >= 0;i--){
count += a[i] * start;
start /= 10;
}
                return count;
}
public static int sum1(int[] a){
int count = 0;
int start = 1000;
for(int i = 0;i < a.length;i++){
count += a[i] * start;
start /= 10;
}
return count;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[4];
while(sc.hasNextInt()){
a[0] = sc.nextInt();
a[1] = sc.nextInt();
a[2] = sc.nextInt();
a[3] = sc.nextInt();
print(a,4);
}
}
}
报错:下标越界,空指针。本机测试是好的,不知道怎么去改。

全部评论

相关推荐

大摆哥:刚好要做个聊天软件,直接让你帮他干活了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务