一个关于局部和全局变量赋值的问题......

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 ?
...全文
350 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
ssqtjffcu 2008-10-13
  • 打赏
  • 举报
回复
是input函数的问题,
String studentNumber=a.next();
String studentName = b.next();
int mathScore=c.nextInt();
int englishScore=d.nextInt();
这四句有错,知道怎么改了吧.

lizhiweiaini 2008-10-13
  • 打赏
  • 举报
回复
不,应该是空实现
lizhiweiaini 2008-10-13
  • 打赏
  • 举报
回复
第一个构造函数不写没事吧
lizhiweiaini 2008-10-13
  • 打赏
  • 举报
回复
问现在能运行了吗
壹个难人 2008-10-13
  • 打赏
  • 举报
回复
不好意思,刚不小心提交了,接着上面的
System.out.println("please enter the englishScore:");
Scanner d=new Scanner(System.in);
int englishScore=d.nextInt();
//把局部变量赋值给全局变量
this.englishScore=englishScore;
二:
input()方法里
Scanner c=new Scanner(System.in);
//直接把输入的值赋给全局变量
this.mathScore=c.nextInt();

System.out.println("please enter the englishScore:");
Scanner d=new Scanner(System.in);
//直接把输入的值赋给全局变量
this.englishScore=d.nextInt();
壹个难人 2008-10-13
  • 打赏
  • 举报
回复
// 构造方法
public Student(){
this("", "", 0.0f, 0.0f);
}
你这里构造的时候mathScore,englishScore都是0.0
所以
public float total(){
float totalScore;
totalScore=mathScore+englishScore;
System.out.println("the totalScore of the student is:"+totalScore);
return 0;
}
你这里totalScore=mathScore+englishScore; totalScore为0.0
两种修改方法:
一:
input()方法里
Scanner c=new Scanner(System.in);
int mathScore=c.nextInt();
//把局部变量赋值给全局变量
this.mathScore=mathScore;

System.out.println("please enter the englishScore:");
Scanner d=new Scanner(System.in);
int englishScore=d.nextInt();

ssqtjffcu 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cjoy4856 的回复:]
引用 5 楼 ssqtjffcu1 的回复:
是input函数的问题,
String studentNumber=a.next();
String studentName = b.next();
int mathScore=c.nextInt();
int englishScore=d.nextInt();
这四句有错,知道怎么改了吧.



将它们强转看一下
[/Quote]

错,楼主是想要把值给成员变量,而这四句是定义了四个局部变量,跟成员变量没关系,所以把前面的类型声明去掉就行了
cjoy4856 2008-10-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ssqtjffcu1 的回复:]
是input函数的问题,
String studentNumber=a.next();
String studentName = b.next();
int mathScore=c.nextInt();
int englishScore=d.nextInt();
这四句有错,知道怎么改了吧.


[/Quote]
将它们强转看一下
lbfly 2008-10-12
  • 打赏
  • 举报
回复
哦,忘记把第一个main方法去掉了[Quote=引用 3 楼 ssqtjffcu1 的回复:]
怎么有两个main函数的???
不明白你在说什么.
[/Quote]
ssqtjffcu 2008-10-12
  • 打赏
  • 举报
回复
怎么有两个main函数的???
不明白你在说什么.
lbfly 2008-10-12
  • 打赏
  • 举报
回复
构造函数啊[Quote=引用 1 楼 chiphuo 的回复:]
public Student(){
this("", "", 0.0f, 0.0f);
}

这个是什么意思?
[/Quote]
chiphuo 2008-10-12
  • 打赏
  • 举报
回复
public Student(){
this("", "", 0.0f, 0.0f);
}

这个是什么意思?

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧