0
import java.io.*;
  import java.util.Scanner;
  class StudentDetails
  {
    int regno,no_of_arrears;
    String name;
    float cgpa;
    Scanner in=new Scanner(System.in);
    StudentDetails()
    {
        regno=0;
        no_of_arrears=0;
        name="";
        cgpa=0.0F;
    }   
    void setStudentDetails()
    {
        System.out.print("\nEnter Student regno : ");
        regno=in.nextInt();
        in.nextLine();
        System.out.print("\nEnter Student name : ");
        name=in.nextLine();
        System.out.print("\nEnter Student CGPA : ");
        cgpa=in.nextFloat();
        System.out.print("\nEnter Student no.of.arrears : ");
        no_of_arrears=in.nextInt(); 
    }
    void printStudentDetails()
    {
        System.out.println(regno+"\t"+name+"\t"+cgpa);  
    }
    boolean isPlacementEligible()
    {
        if(cgpa >= 7.5 && no_of_arrears==0)
        {   
            return true;
        }
        else
        {
            return false;
        }
    }

  }
  class Student
  { 
    public static void main(String args[])
    {
        int n=0,i,count=0,ch;
        Scanner in=new Scanner(System.in);
        System.out.println("\t\t\tSTUDENT LIST");
        System.out.println("\t\t\t------------");
        System.out.println("1.Set Student Details\n2.Print all Students\n3.Print   Placement Eligible Student\n4.Exit");    
        do{
            System.out.print("\nEnter the choice : ");
            ch=in.nextInt();
            StudentDetails s1=new StudentDetails();
            StudentDetails s[]={s1};
            switch(ch){
                case 1:
                    System.out.print("\nEnter no of students : ");
                    n=in.nextInt();
                    s=new StudentDetails[n];
                    for(i=0;i<n;i++)
                    {   
                        s[i]=new StudentDetails();
                        System.out.print("\nEnter Details Of Student "+(i+1));
                        System.out.println("\n--------------------------");
                        s[i].setStudentDetails();
                        if(s[i].isPlacementEligible())
                        {
                            count++;
                        }
                    }

                    break;
                case 2:
                    System.out.println(n);
                    System.out.println("\nDetails of All Students ");
                    System.out.println("\n------------------------");
                    System.out.println("Total no of students : "+n);
                    System.out.println("Regno\tName\tCGPA");
                    System.out.println("-----\t----\t----");
                    for(i=0;i<n;i++)
                    {
                        s[i].printStudentDetails();

                    }   
                    break;
                case 3:
                    System.out.println("\nDetails of Eligible Students ");
                    System.out.println("\n-----------------------------");
                    System.out.println("Eligible for placements : "+count);
                    System.out.println("Regno\tName\tCGPA");
                    System.out.println("-----\t----\t----");
                    for(i=0;i<n;i++)
                    {
                        if(s[i].isPlacementEligible())
                        {
                            s[i].printStudentDetails();
                        }
                    }
                    break;
                case 4:
                    System.exit(0);
                default:
                    System.out.println("Enter correct choice ");
            }   
        }while(ch!=4);
    }   
}

输出:STUDENT LIST ------------ 1.Set Student Details 2.Print all Students 3.Print Placement Eligible Student 4.Exit

输入选项:1

输入学生人数:1

输入学生 1 的详细信息

输入学生 regno : 1

输入学生姓名:sjnsj

进入学生 CGPA : 9.8

输入学生欠款:0

输入选项:2 1

所有学生的详细信息


学生总数:1

Regno 名称 CGPA ----- ---- ---- 0 0.0

输入选项:4

在这里,我在 case1 中创建了对象数组,但在 case 2 中无法读取

4

2 回答 2

0

您在 do-while 循环中声明数组,因此每次循环迭代都会超出范围。

即使您在循环的前一次迭代中实例化了它case1,您仍然会在循环的每个其他迭代中访问一个完全不同的数组。

如果您想为每次迭代使用相同的数组,请在循环之外声明它

StudentDetails s1=new StudentDetails();
StudentDetails s[]={s1};
do
{
    ...
}
于 2013-10-01T16:14:57.993 回答
0

发生这种情况是因为您s在循环体内声明了变量。像这样更改您的代码:

    ...
    System.out.println("1.Set Student Details\n2.Print all Students\n3.Print   Placement Eligible Student\n4.Exit");  
    StudentDetails s[]= null;  
    do{
        System.out.print("\nEnter the choice : ");
        ch=in.nextInt();
        StudentDetails s1=new StudentDetails();
        s[]={s1};
        switch(ch){
     ....
于 2013-10-01T16:15:40.253 回答