一个关于局部和全局变量赋值的问题......
lbfly 2008-10-12 10:25:48 import java.util.Scanner;
public class Student {
String studentNumber;
String studentName;
float mathScore;
float englishScore;
// 构造方法
public Student(){
this("", "", 0.0f, 0.0f);
}
public Student(String newStudentNumber,
String newStudentName, float newMathScore,float newEnglishScore){
studentNumber=newStudentNumber;
studentName=newStudentName;
mathScore=newMathScore;
englishScore=newEnglishScore;
}
//input()方法
public void input(){
System.out.println("please enter the studentNumber :");
Scanner a=new Scanner(System.in);
String studentNumber=a.next();
System.out.println("please enter the studentName:");
Scanner b = new Scanner(System.in);
String studentName = b.next();
System.out.println("please enter the mathScore:");
Scanner c=new Scanner(System.in);
int mathScore=c.nextInt();
System.out.println("please enter the englishScore:");
Scanner d=new Scanner(System.in);
int englishScore=d.nextInt();
System.out.println("the information of the students are:" );
System.out.println("studentNumber "+"studentName "
+"mathScore "+"englishScore ");
System.out.println(studentNumber+" "+studentName+" "
+mathScore+" "+englishScore);
}
//total()方法
public float total(){
float totalScore;
totalScore=mathScore+englishScore;
System.out.println("the totalScore of the student is:"+totalScore);
return 0;
}
public static void main(String args[]){
Student std=new Student();
std.total();
}
public static void main(String args[]){
Student studentArry[];
studentArry=new Student[3];
for(int i=0;i <studentArry.length;i++){
studentArry[i]=new Student();
}
//不能少否则出现空指针,若少了程序只是创建了数组,并没有创建数组需要保存的对象
studentArry[0].input();
studentArry[0].total();
studentArry[1].input();
studentArry[1].total();
studentArry[2].input();
studentArry[2].total();
}
}
为什么从键盘输入的mathscore和english没有被对象名.total()引用了呢?
而mathscore和english在.total()中是被赋值为0 ?