java定义一个实数数组用来存放学生成绩

import java.util.Scanner;
public class score {

/**
* @param args
*/
public static void main(String[] args) {

Scanner reader=new Scanner(System.in);
System.out.println("输入用户个数:");
int score=reader.nextInt();
int score1[]=new int[score];
System.out.println("输入用户成绩:");
for(int i=1;i<=score1.length;i++){
double s=reader.nextDouble();

}
System.out.println("输入查找学生");
int n=reader.nextInt();
if(n==score){
System.out.println("第"+score+"学生成绩是:"+score1[score]);
}
}

}
我执行不到最后一步~最后需要询问用户查询结果

主要是下标做了,记住数组是用0开始,现在调通了
import java.util.Scanner;

public class Admin {

public static void main(String... args) {
Scanner reader = new Scanner(System.in);
System.out.println("输入用户个数:");
int score = reader.nextInt();
double score1[] = new double[score];
System.out.println("输入用户成绩:");
for (int i = 0; i < score1.length; i++) {
double s = reader.nextDouble();
score1[i] = s;
}
System.out.println("输入查找学生");
int n = reader.nextInt();
if (n == score) {
System.out.println("第" + score + "学生成绩是:" + score1[score - 1]);
}
}
}追问

可以了
score1[i] = s;这个什么意思啊

追答

要给数组赋值啊,要不你创建他做什么啊

追问

赋值以后在if语句里面没有赋值的s啊。但是少这句就不能查询。有这句就可以查询~这个不太明白。我新手讲解一下吧

追答

没有赋值就相当于你只是创建了个数组,里面是空的(默认值是0.0)
double score1[] = new double[score];
然后你查询,肯定查不到啊.

追问

谢谢。!!!感谢!!给你分

温馨提示:内容为网友见解,仅供参考
第1个回答  2012-04-25
import java.util.Scanner;
public class score{

/**
* @param args
*/
public static void main(String[] args) {

Scanner reader=new Scanner(System.in);
System.out.println("输入用户个数:");
int score=reader.nextInt();
double score1[]=new double[score];
System.out.println("输入用户成绩:");
for(int i=0;i<score1.length;i++){
double s=reader.nextDouble();
score1[i] = s;

}
System.out.println("输入查找学生");
int n=reader.nextInt();
System.out.println("第"+n+"学生成绩是:"+score1[n-1]);

}

}
最后应该是这样吧,要不永远只能查最后一名学生的成绩。
相似回答