langlanlang
2015-04-25 16:53
public class Helloworld
{
public static void main (String [] args){
Helloworld hello=new Helloworld();
int x[]={89,-23,64,91,119,52,73};
x=hello.getAraray(3, 7);
public int [] getAraray(int a,int b){
int y[]= new int [b];
int max=y[0];
System.out.println("考试成绩前3名分数为:");
for (int x=1;x<=a;x++){
for(int i=0;i<b;i++){
if(max<=y[i]){
int c= max;
max=y[i];
if(max>100&&max<0){max =c;}
}
}System.out.println(max);
}return y;
}
}
你好,你这个代码有问题。
第一,输出都是0 ,因为你在方法里面知识new了一个数组,并没有将数组传递给方法里面。我给你改完了。参考
public class HelloWorld {
public static void main(String[] args) {
HelloWorld hello = new HelloWorld();
int x[] = { 89, -23, 64, 91, 119, 52, 73 };
x = hello.getAraray(3, 7,x);
}
public int[] getAraray(int a, int b,int d[]) {
int y[] = new int[b];
y=d;
int max = y[0];
System.out.println("考试成绩前3名分数为:");
for (int x = 1; x <= a; x++) {
for (int i = 0; i < b; i++) {
if (max <= y[i]) {
int c = max;
max = y[i];
if (max > 100 && max < 0) {
max = c;
}
}
}System.out.println(max);
}
return y;
}
}
第二个,你这个代码根本满足不了输出前三名,因为你这个是做的比较大小,那就意味着,无论你运行多少次,max都是最大值,而你又输出max,那一定会输出3个119的。
第三,给你一个这个题的思路,我建议你的方法里面可以将数组排序,排序升序或者降序,假如降序排列然后输出前三个,不就是成绩的前三名了嘛。这个是我的思路,你也可以发掘更好的办法。
int y[]= new int [b];你数组里面没有值 ,只是有个数组的长度,所以int y[]里的7个值全是int类型的初值0,结果肯定是0啊,写的什么破玩意 乱七八糟的。
Java入门第一季(IDEA工具)
1168153 学习 · 18754 问题
相似问题