Topic: 一个类似于结构的数组问题

  Print this page

1.一个类似于结构的数组问题 Copy to clipboard
Posted by: mtdhwgij
Posted on: 2006-07-04 17:46

题目:输入几个学生的姓名,性别,年龄,并显示。
程序代码如下,能通过编译但通不能按预想运行。


import javax.swing.JOptionPane;
import java.io.*;
class student
{
String sname,ssex,sage;
}
public class study
{
public static void main(String[] args)
{
student stu[]=new student[3];
for(int i=0;i<3;i++)
{

stu[i].sname=JOptionPane.showInputDialog("Enter the "+i+" student name:");
stu[i].ssex=JOptionPane.showInputDialog("Enter the sex of the student:");
stu[i].sage=JOptionPane.showInputDialog("Enter the age of the student:");


}
for(int i=0;i<3;i++)
{
System.out.println("the"+i+"student name is:"+stu[i].sname);
System.out.println("sex:"+stu[i].ssex);
System.out.println("age:"+stu[i].sage);
}

System.exit(0);
}
}


2.Re:一个类似于结构的数组问题 [Re: mtdhwgij] Copy to clipboard
Posted by: mtdhwgij
Posted on: 2006-07-04 17:49

程序经简化成单个学生后能够正常编译运行,但就不符合题目要求了。

import javax.swing.JOptionPane;
import java.io.*;
class student
{
String sname,ssex,sage;
}
public class study
{
public static void main(String[] args)
{
student stu=new student();
stu.sname=JOptionPane.showInputDialog("Enter the student name:");
stu.ssex=JOptionPane.showInputDialog("Enter the sex of the student:");
stu.sage=JOptionPane.showInputDialog("Enter the age of the student:");
System.out.println("the student name is:"+stu.sname);

}
}


3.Re:一个类似于结构的数组问题 [Re: mtdhwgij] Copy to clipboard
Posted by: why
Posted on: 2006-07-04 20:12

for(int i=0;i<3;i++)
{
// student stu[]=new student[3]; does not create the student objects, i.e. stu[i] is null; so...
stu[i] = new student();
stu[i].sname=JOptionPane.showInputDialog("Enter the "+i+" student name:");

4.Re:一个类似于结构的数组问题 [Re: mtdhwgij] Copy to clipboard
Posted by: mtdhwgij
Posted on: 2006-07-04 20:40

  老大,你太强了,现在可以正常使用了,谢了哈!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923