java数组输出问题
代码如下:publicclassStudent{privatedoublestudentnumber;privateStringname;privatedoubleage...
代码如下:
public class Student {
private double studentnumber;
private String name;
private double age;
private String studentclass;
public double getNumber(){
return studentnumber;
}
public void setNumber(double studentnumber){
this.studentnumber=studentnumber;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public double getAge(){
return age;
}
public void setAge(double age){
this.age=age;
}
public String getStudentClass(){
return studentclass;
}
public void setStudentClass(String studentclass){
this.studentclass=studentclass;
}
public void getInformation(){
Student si= new Student();
System.out.println("学生学号为:"+si.getNumber()+"学生姓名为:"+si.getName()+"学生姓名为:"+si.getAge()+"学生姓名为:"+si.getStudentClass());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
import java.util.Arrays;
import java.util.Scanner;
public class StudentDisplayDemo {
public static void main(String[] args) {
int i;
Student[] students = new Student[5];
for (i=0;i<5;i++){
Scanner scan= new Scanner(System.in);
//对象数组
Student student= new Student();
System.out.println("请输入第" + (i + 1) + "位同学的信息");
System.out.print("学号:");
//student.setNumber(scan.next());
String studentnumber= scan.next();
System.out.print("姓名:");
student.setName(scan.next());
//String name= scan.next();
System.out.print("年龄:");
String age= scan.next();
System.out.print("班级:");
student.setStudentClass(scan.next());
//String studentclass= scan.next();
students[i]=student;
}
for(i=0;i<5;i++){
System.out.println("第"+(i+1)+"位学生:");
students[i].getInformation();
}
}
}
为何输出结果:
求解答 展开
public class Student {
private double studentnumber;
private String name;
private double age;
private String studentclass;
public double getNumber(){
return studentnumber;
}
public void setNumber(double studentnumber){
this.studentnumber=studentnumber;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public double getAge(){
return age;
}
public void setAge(double age){
this.age=age;
}
public String getStudentClass(){
return studentclass;
}
public void setStudentClass(String studentclass){
this.studentclass=studentclass;
}
public void getInformation(){
Student si= new Student();
System.out.println("学生学号为:"+si.getNumber()+"学生姓名为:"+si.getName()+"学生姓名为:"+si.getAge()+"学生姓名为:"+si.getStudentClass());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
import java.util.Arrays;
import java.util.Scanner;
public class StudentDisplayDemo {
public static void main(String[] args) {
int i;
Student[] students = new Student[5];
for (i=0;i<5;i++){
Scanner scan= new Scanner(System.in);
//对象数组
Student student= new Student();
System.out.println("请输入第" + (i + 1) + "位同学的信息");
System.out.print("学号:");
//student.setNumber(scan.next());
String studentnumber= scan.next();
System.out.print("姓名:");
student.setName(scan.next());
//String name= scan.next();
System.out.print("年龄:");
String age= scan.next();
System.out.print("班级:");
student.setStudentClass(scan.next());
//String studentclass= scan.next();
students[i]=student;
}
for(i=0;i<5;i++){
System.out.println("第"+(i+1)+"位学生:");
students[i].getInformation();
}
}
}
为何输出结果:
求解答 展开
2015-06-06
展开全部
import java.util.Scanner;
public class Student
{
private double studentnumber;
private String name;
private double age;
private String studentclass;
public double getNumber ()
{
return studentnumber;
}
public void setNumber ( double studentnumber )
{
this.studentnumber = studentnumber;
}
public String getName ()
{
return name;
}
public void setName ( String name )
{
this.name = name;
}
public double getAge ()
{
return age;
}
public void setAge ( double age )
{
this.age = age;
}
public String getStudentClass ()
{
return studentclass;
}
public void setStudentClass ( String studentclass )
{
this.studentclass = studentclass;
}
public void getInformation ()
{
System.out.println ("学生学号为:" + this.studentnumber + " 学生姓名为:" + this.name + " 学生年龄为:" + this.age + " 学生班级为:" + this.studentclass);
}
public static void main ( String[] args )
{
Student[] students = new Student[5];
Scanner scan = new Scanner (System.in);
for ( int i = 0; i < 5; i++ )
{
System.out.println ("请输入第" + ( i + 1 ) + "位同学的信息");
Student student = new Student ();
System.out.print ("学号:");
student.setNumber (scan.nextDouble ());
System.out.print ("姓名:");
student.setName (scan.next ());
System.out.print ("年龄:");
student.setAge (scan.nextDouble ());
System.out.print ("班级:");
student.setStudentClass (scan.next ());
students[i] = student;
}
scan.close ();
for ( int i = 0; i < 5; i++ )
{
System.out.println ("第" + ( i + 1 ) + "位学生:");
students[i].getInformation ();
}
}
}
追问
首先,谢谢您的回答,完美的解决了我的问题。
然后我有几个问题:
如果这样做的话,get方法就会毫无意义,请问我开始时利用对象调用si.getNumber等为什么会出现无法输出正确值的错误?那么用了您的方法之后还需要定义get方法吗?
我出现这种问题主要是哪里有模糊的点,主要是getInformation方法还是之后的scanner输入?
请您不吝赐教,谢谢!
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询