java中对象数组
importjava.io.*;importjava.lang.*;classStudent{Stringname;intage;intnum;//学号doublehig...
import java.io.*;
import java.lang.*;
class Student
{
String name;
int age;
int num;//学号
double high;//身高
public void display()
{
System.out.println("姓名"+name+"年龄"+age+"身高"+high+"学号"+num);
}
public void setStudent(String n,int a,int nu,double h)
{
name=n;
age=a;
num=nu;
high=h;
}
}
public class StudentInformation {
public static void main(String args[]) throws IOException
{
Student st =new Student[5];
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);
for(int i=0;i<6;i++)
st[i].display();
System.out.println("请输入要查询学生的姓名");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str=buf.readLine();
for(int i=0;i<6;i++)
{
if(st[i].name.equals(str))
{
System.out.println("所要查找学生为");
st[i].display();
}
}
}
}
本人刚接触java 大家帮下忙 帮看下st对象数组初始化哪又问题 谢谢~! 展开
import java.lang.*;
class Student
{
String name;
int age;
int num;//学号
double high;//身高
public void display()
{
System.out.println("姓名"+name+"年龄"+age+"身高"+high+"学号"+num);
}
public void setStudent(String n,int a,int nu,double h)
{
name=n;
age=a;
num=nu;
high=h;
}
}
public class StudentInformation {
public static void main(String args[]) throws IOException
{
Student st =new Student[5];
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);
for(int i=0;i<6;i++)
st[i].display();
System.out.println("请输入要查询学生的姓名");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str=buf.readLine();
for(int i=0;i<6;i++)
{
if(st[i].name.equals(str))
{
System.out.println("所要查找学生为");
st[i].display();
}
}
}
}
本人刚接触java 大家帮下忙 帮看下st对象数组初始化哪又问题 谢谢~! 展开
3个回答
展开全部
Student st =new Student[5];
首先数组定义错误 st是一个Student数组 初始化的时候错了,应该是:
Student[] st =new Student[5];
其次:
Student st =new Student[5];
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);
调用对象数组中Student对象的方法前要对数组中的Student对象进行初始化,应该在
Student st =new Student[5];后加上
for (int i = 0; i < st.length; i++) {
st[i] = new Student();
}
3:java中数组下标从0开始,长度为5的数组进行循环应该是从0到4,如果用st[5]会出现数组越界。
我简单改了下你的代码:
public static void main(String args[]) throws IOException {
Student[] st = new Student[5];
for (int i = 0; i < st.length; i++) {
st[i] = new Student();
}
st[0].setStudent("WangZhen", 20, 1, 1.76);
st[1].setStudent("YangZhen", 21, 2, 1.66);
st[2].setStudent("Wangqiang", 19, 3, 1.86);
st[3].setStudent("XiaoMing", 18, 4, 1.70);
st[4].setStudent("XiaoHong", 22, 5, 1.74);
for (int i = 0; i < 5; i++)
st[i].display();
System.out.println("name");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (int i = 0; i < 5; i++) {
if (st[i].name.equals(str)) {
System.out.println("result");
st[i].display();
}
}
}
首先数组定义错误 st是一个Student数组 初始化的时候错了,应该是:
Student[] st =new Student[5];
其次:
Student st =new Student[5];
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);
调用对象数组中Student对象的方法前要对数组中的Student对象进行初始化,应该在
Student st =new Student[5];后加上
for (int i = 0; i < st.length; i++) {
st[i] = new Student();
}
3:java中数组下标从0开始,长度为5的数组进行循环应该是从0到4,如果用st[5]会出现数组越界。
我简单改了下你的代码:
public static void main(String args[]) throws IOException {
Student[] st = new Student[5];
for (int i = 0; i < st.length; i++) {
st[i] = new Student();
}
st[0].setStudent("WangZhen", 20, 1, 1.76);
st[1].setStudent("YangZhen", 21, 2, 1.66);
st[2].setStudent("Wangqiang", 19, 3, 1.86);
st[3].setStudent("XiaoMing", 18, 4, 1.70);
st[4].setStudent("XiaoHong", 22, 5, 1.74);
for (int i = 0; i < 5; i++)
st[i].display();
System.out.println("name");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (int i = 0; i < 5; i++) {
if (st[i].name.equals(str)) {
System.out.println("result");
st[i].display();
}
}
}
展开全部
//如果你是初学JAVA,建议你看看这段代码,里面有一些基本技巧
//有疑问可以问我!
import java.io.*;
public class StudentInformation {
public static void main(String args[]) throws IOException {
Student[] st = new Student[5];// 第一个错误
int i = 0;
st[i++] = new Student().setStudent("WangZhen", 20, 1, 1.76);
st[i++] = new Student().setStudent("YangZhen", 21, 2, 1.66);
st[i++] = new Student().setStudent("Wangqiang", 19, 3, 1.86);
st[i++] = new Student().setStudent("XiaoMing", 18, 4, 1.71);
st[i++] = new Student().setStudent("XiaoHong", 22, 5, 1.74);
for (i = 0; i < st.length; i++)
st[i].display();
System.out.println("请输入要查询学生的姓名");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (i = 0; i < st.length; i++) {
if (st[i].name.equals(str)) {
System.out.println("所要查找学生为");
st[i].display();
}
}
}
}
class Student {
String name;
int age;
int num;// 学号
double high;// 身高
public void display() {
System.out.println("姓名:" + name + "\t年龄:" + age + "\t身高:" + high + "\t学号:" + num);
}
public Student setStudent(String n, int a, int nu, double h) {
name = n;
age = a;
num = nu;
high = h;
return this;
}
}
//有疑问可以问我!
import java.io.*;
public class StudentInformation {
public static void main(String args[]) throws IOException {
Student[] st = new Student[5];// 第一个错误
int i = 0;
st[i++] = new Student().setStudent("WangZhen", 20, 1, 1.76);
st[i++] = new Student().setStudent("YangZhen", 21, 2, 1.66);
st[i++] = new Student().setStudent("Wangqiang", 19, 3, 1.86);
st[i++] = new Student().setStudent("XiaoMing", 18, 4, 1.71);
st[i++] = new Student().setStudent("XiaoHong", 22, 5, 1.74);
for (i = 0; i < st.length; i++)
st[i].display();
System.out.println("请输入要查询学生的姓名");
String str;
BufferedReader buf;
buf = new BufferedReader(new InputStreamReader(System.in));
str = buf.readLine();
for (i = 0; i < st.length; i++) {
if (st[i].name.equals(str)) {
System.out.println("所要查找学生为");
st[i].display();
}
}
}
}
class Student {
String name;
int age;
int num;// 学号
double high;// 身高
public void display() {
System.out.println("姓名:" + name + "\t年龄:" + age + "\t身高:" + high + "\t学号:" + num);
}
public Student setStudent(String n, int a, int nu, double h) {
name = n;
age = a;
num = nu;
high = h;
return this;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Student []st =new Student[5];
for(int i=0;i++;i<st.length){
st[i]=new Student();
}
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);
对象数组中的对象没有实例化
把 setStudent中的内容放到构造方法中在实例化的时候传值比较好
for(int i=0;i++;i<st.length){
st[i]=new Student();
}
st[1].setStudent("WangZhen", 20, 1, 1.76);
st[2].setStudent("YangZhen", 21, 2, 1.66);
st[3].setStudent("Wangqiang", 19, 3, 1.86);
st[4].setStudent("XiaoMing", 18, 4, 1.70);
st[5].setStudent("XiaoHong", 22, 5, 1.74);
对象数组中的对象没有实例化
把 setStudent中的内容放到构造方法中在实例化的时候传值比较好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询