从一个法阵设置和其他方法得到 [英] set in array from one method and get from another method

查看:134
本文介绍了从一个法阵设置和其他方法得到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从学生类数组

 学生[] = STU新生[100];

我在班级学生运,我想从插入方法名和code 插入数组 STU

到现在为止一切正常,但是当我想要得到我的名字和code从另一个方法类似的类的学生运删除的,它表明我的数组 STU 是空的。哪里有问题?如果我DELET行结果
     STU [0] =新的学生();
DELET 的方法豆网显示错误!

 公共类studentop {     学生[] = STU新生[100];    私人INT计数器= 0;
    INT CON = 0;
    公众诠释EDD;    公共无效插入(INT edame){        答串;
        扫描仪输入=新的扫描仪(System.in);
        对于(INT I = edame; I< 100;我++){
            的System.out.println(增加更多的学生吗?);
            ANS = input.next();
            如果(ans.equals(Y)){                串thename;                STU [I] =新的学生();
                的System.out.println(插入件名+第(i + 1)+:);                thename = input.next();
                STU [I] .setname(thename);                的System.out.println(插入code+(I + 1)+:);
                诠释了code;
                在code = input.nextInt();
                STU [I] .SET code(在code);                的System.out.println(STU [I]。获得code());                为(中间体m为0; M&下; = 3; M +){
                    的System.out.println(加标记+(M + 1)+?);
                    串ANS2;
                    ANS2 = input.next();
                    如果(ans2.equals(Y)){
                        开关(米){                            情况下0:
                                的System.out.println(标记+(M + 1)+:);
                                INT标志;
                                马克= input.nextInt();
                                STU [I] .setmark(标记);
                                打破;                            情况1:
                                的System.out.println(标记+(M + 1)+:);
                                // INT标志;
                                马克= input.nextInt();
                                STU [I] .setMark2(标记);
                                打破;                            案例2:
                                的System.out.println(标记+(M + 1)+:);
                                // INT标志;
                                马克= input.nextInt();
                                STU [I] .setMark3(标记);
                                打破;                            案例3:
                                的System.out.println(标记+(M + 1)+:);
                                // INT标志;
                                马克= input.nextInt();
                                STU [I] .setMark4(标记);
                                打破;
                        }
                    }
                    其他{
                        打破;
                    }
                }
                的System.out.println(STU [I] .getname()); // aztarighe得到名称AZ
            }
            其他{
                edame = I;
                EDD = edame;
                打破;
            }
        } //结束了
    }
    公共无效删除(){
        STU [0] =新的学生(); //如果我DELET这条线就显示错误!
        的System.out.println(STU [0]。获得code());
    }
}

和这里是我的学生班

 公共类学生{
    公共字符串名称;
    公众诠释code;
    公共双标志;
    私人双MARK2;
    私人双MARK3;
    私人双mark4;    公共无效setname可以(字符串SOURCENAME){
        名称= SOURCENAME;
    }
    公共字符串的getName(){
        返回名称;
    }


解决方案

这不是空的,但是,当你试图从正在创建一个新的学生,这就是为什么它似乎是空数组学生:

 公共无效删除()
{
    STU [0] =新的学生(); //这里你删除旧的学生
    的System.out.println(STU [0]。获得code());
}

相反,你可以使用:

 公共无效删除()
{
    的System.out.println(STU [0]。获得code());
}

I have made an array from class student

student [] stu=new student[100];

I'm in class student op and I want to insert name and code from method insert to array stu.

Until now everything worked, but when I want to get my name and code from another method like delete in class student op, it shows that my array stu is empty. Where is the problem? if I delet the line
stu[0]=new student(); in delet method Net beans show an error!!

public class studentop {

     student [] stu=new student[100];

    private int counter=0;
    int con=0;
    public int edd;

    public void insert(int edame){

        String ans;
        Scanner input=new Scanner(System.in);
        for(int i=edame;i<100;i++){
            System.out.println("add more student? ");
            ans=input.next();
            if(ans.equals("y")){

                String thename;

                stu[i]=new student();
                System.out.println("insert name  "+(i+1)+" : ");

                thename = input.next();
                stu[i].setname(thename);

                System.out.println("insert code  "+(i+1)+" : ");
                int thecode;
                thecode = input.nextInt();
                stu[i].setcode(thecode);

                System.out.println(stu[i].getcode());

                for(int m=0;m<=3;m++){
                    System.out.println("add mark  "+(m+1)+" ? ");
                    String ans2;
                    ans2=input.next();
                    if(ans2.equals("y")){
                        switch (m){

                            case 0:
                                System.out.println("mark"+(m+1)+" : ");
                                int mark;
                                mark=input.nextInt();
                                stu[i].setmark(mark);
                                break;

                            case 1:
                                System.out.println("mark"+(m+1)+" : ");
                                //  int mark;
                                mark=input.nextInt();
                                stu[i].setMark2(mark);
                                break;

                            case 2:
                                System.out.println("mark"+(m+1)+" : ");
                                //  int mark;
                                mark=input.nextInt();
                                stu[i].setMark3(mark);
                                break;

                            case 3:
                                System.out.println("mark"+(m+1)+" : ");
                                // int mark;
                                mark=input.nextInt();
                                stu[i].setMark4(mark);
                                break;
                        }
                    }
                    else{
                        break;
                    }
                }
                System.out.println(stu[i].getname());//aztarighe get name az 
            }
            else {
                edame=i;
                edd=edame;
                break;
            }
        }//end for
    }
    public void delete(){
        stu[0]=new student();//if I delet this line it shows an error!
        System.out.println(stu[0].getcode());
    }
}

and here is my class student

public class student {
    public String name;
    public int code;
    public double mark;
    private double mark2;
    private double mark3;
    private double mark4;

    public void setname(String sourcename){
        name=sourcename;
    }
    public String getname(){
        return name;
    }

解决方案

It is not empty, however, when you try to get the student from the array you're creating a new student that's why it appears to be empty:

public void delete()
{
    stu[0]=new student(); //here you're deleting your old student
    System.out.println(stu[0].getcode());
}

Instead you could use:

public void delete()
{
    System.out.println(stu[0].getcode());
}

这篇关于从一个法阵设置和其他方法得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆