日期:2014-05-20  浏览次数:20865 次

一个JAVA初学者关于JAVA数据类型的一个疑问
正如我所写的,我写的Student类里面有一个long型的实例变量ID(请继续往下看)
public class Student{
private long ID;
private String name;
private String sex;
private double[] grades;
private double average;

public long getID() {
return ID;
}
public Student(long a,String b,String c,double...list){
ID=a;
name=b;
sex=c;
grades=list;
double d=0;
for(double e:list){
d=d+e;
}
average=d/list.length;
}
public String toString(){
String a=ID+" "+name+" "+sex+" ";
String b="";
for(int i=0;i<10;i++){
String c=grades[i]+" ";
b=b+c;
}
b=a+b+" "+"平均分:"+average;
return b;
}
}
但在这个程序里面我创建了5个Student,用了十二位的学号,但是Eclipse报错说这个数值超过了int型数值的范围,请问这是哪里出问题了?????
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class wenjian {
/**
* @param args
*/
public static void main (String[] args) {
// TODO Auto-generated method stub
ArrayList<Student> student=new ArrayList<Student>();
Student a=new Student(201100300130, "李红红","女", 68,66,74,85,87,88,89,90,93,98);
Student b=new Student(201100300128, "张亮","男", 68,66,74,85,87,88,89,90,93,98);
Student c=new Student(201100300131, "李乐","男", 68,66,74,85,87,88,89,90,93,98);
Student d=new Student(201100300132, "王刚","男", 68,66,74,85,87,88,89,90,93,98);
Student e=new Student(201100200526, "刘晓庆","女", 68,66,74,85,87,88,89,90,93,98);
student.add(a);
student.add(b);
student.add(c);
student.add(d);
student.add(e);
try{
  String file="studentsdata.txt";
  FileWriter fw=new FileWriter(file);
  BufferWriter bf=new BufferWriter(fw);
  PrintWriter pw=new PrintWriter(bf);
  pw.println("学号"+" "+"姓名"+" "+"性别"+" "+
  "成绩"+" "+"平均"+" "+"成绩");
  for(int i=1;i<=student.size();i++){
  pw.println(student.get(i-1));
  }
  Scanner scan=new Scanner(System.in);
  System.out.println("请输入要查询的学生的学号:");
  long id=scan.nextLong();
  Searching.searching(student,id);
}
catch(IOException f){
System.out.println("文件操作出现错误!");
}
catch(Exception g){
System.out.println("文件操作出现错误!");
}
}
 

大侠要帮帮我啊

------解决方案--------------------
在你12位的数字后面加个L就没错了?
------解决方案--------------------
学号的话用String不是更合适吗?
------解决方案--------------------
Student a=new Student(201100300130L, "李红红","女", 68,66,74,85,87,88,89,90,93,98);
就是每个学号后面加大写L
------解决方案--------------------
整数的默认是 int的 所以要加个L
------解决方案--------------------
是的 后面要加L
------解决方案--------------------
恩 后面需要一个L,可是个人觉得 学号应该用String类型更加适合些
------解决方案--------------------
因为它更像个字符串,~~~~~~~~~~~~
而且往数据库里比较好存吧