【问题标题】:something wrong while calling a method调用方法时出现问题
【发布时间】:2014-11-28 13:03:14
【问题描述】:
Scanner kb = new Scanner(System.in);
student [] s =new student[100];
int index = 0;  

for(int i=0;i<100;i++){   
   System.out.println("Do you want to add new student \n \t if not enter (n) or (N) to exit  ");

   String id=kb.next();

   if(id.equals("n") || id.equals("N")){
       System.exit(0);
   }
   else {
     for(int q = 1; q<4;q++){
         System.out.println("Enter Quiz "+q+" score");
         double score=kb.nextInt();
         s[i].addQuiz(score);
     }
   }
   addStudent(s , i , id );

   }
}

public void addStudent(student[] old, int index , String id){       
    old [index]= new student(id);
}

java 在添加学生方法调用时给我错误 如果我的数组传递是否正确,我不相信:(

【问题讨论】:

  • 包含一个堆栈跟踪怎么样?

标签: java arrays object methods


【解决方案1】:

那是因为你的变量 i 超出了 for 循环的范围。因此,在关闭 for 循环之前添加此语句 addStudent(s , i , id );

另外,我相信您是从静态的 main 方法调用 addStudent 并调用非静态方法,在这种情况下,将方法定义更改为:

public static void addStudent(student[] old, int index, String id) {

这应该可行。

【讨论】:

  • @zainab.a.k 更新您有问题的代码后将addstudent 调用引入for 循环。我也在你的代码中发现了奇怪的东西,并相应地更新了我的答案。添加您遇到的错误。
  • 'i' 在他的 for 循环中没有超出范围,我认为您正在混合 2 个 for 循环。
  • else { for(int q = 1; q
  • @zainab.a.k 请在更改后用您现在拥有的任何内容更新问题,并告诉我们您遇到了什么错误。
  • 我不知道如何更新问题,错误是“非静态方法 addStudent 不能从静态上下文中引用”
猜你喜欢
  • 2021-05-28
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 2017-04-08
  • 2015-09-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多