如何调用非静态方法(数组)

时间:2014-11-14 08:01:19

标签: java arrays methods non-static

我正在尝试调用我的add方法来为我的数组添加分数 到目前为止我已经得到了这个,但我一直收到一个错误,说myQuiz从未被初始化。 .................................................. ....................................

import java.util.Scanner;
public class Assignment7 {
public static void main (String[] args) {
       //new scanner
       Scanner in = new Scanner (System.in);
       String choice;
       char command;
       // print the menu
       int count = 0;
       int counter = 0;
       printMenu();
       int array[] = new int[0];
      //do while loop testing using user input 
       do{
           // ask a user to choose a command
           System.out.println("\nPlease enter a command or type ?");
           choice = in.next().toLowerCase();
           command = choice.charAt(0);
           //start switch statement for user input cases
           switch (command)
            {
                 switch (command)
            {
                 case 'n':  //ask and read the size of the input array and name
                     System.out.print("\n\t n [Create a new Quiz]: ");
                     System.out.print("\n\t  [Input the size of quizzes]: ");
                     int num=in.nextInt(); // read the integer
                     array = new int[num];
                     System.out.print("\n\t  [Input the name of the student]: ");
                     String name = in.next(); //read name
                     Quiz myQuiz = new Quiz(num, name);
                      break;
                 case 'a': //  ask and add score to array
                      System.out.print("\n\t a [Add a score]: ");
                          array[count] = in.nextInt();
                          myQuiz.add(array[count]);
                          counter++;
                      break;
                     /*
                 case 'a': //  ask and add score to array
                      System.out.print("\n\t a [Add a score]: ");
                          array[count] = in.nextInt();
                          myQuiz.add(array[count]); //HELP
                          counter++;
                      break;

我的Quiz.java添加方法

public class Quiz {

private int count;
private int[] scores;
private String name;    
public Quiz(int a,String name){
 scores = new int [a];
 for (int i = 0; i<scores.length; i++){
     scores[i] = -1;
 }
 this.count = 0;
 this.name = "";
 }
public void add(int b){
 for(int i : scores){
 if(scores[i] == count){
    System.out.println("Array is full. The value " + b + " cannot be added.");
 }
 else {
     scores[count] = b;
     count++;
 }

2 个答案:

答案 0 :(得分:0)

您创建一个空数组:

int array[] = new int[0];

并尝试为其指定数字:

array[count] = in.nextInt();

它无法正常工作。

你必须给它一个正长度。

答案 1 :(得分:0)

更仔细地观察您的范围:您在switch-case语句的一个分支中定义myQuiz,但您也可以在另一个分支中访问它。如果访问case 'a'会发生什么? myQuiz变量在那里是未知的!

             case 'n':  //ask and read the size of the input array and name
                 System.out.print("\n\t n [Create a new Quiz]: ");
                 System.out.print("\n\t  [Input the size of quizzes]: ");
                 int num=in.nextInt(); // read the integer
                 array = new int[num];
                 System.out.print("\n\t  [Input the name of the student]: ");
                 String name = in.next(); //read name
                 Quiz myQuiz = new Quiz(num, name);
                  break;
             case 'a': //  ask and add score to array
                  System.out.print("\n\t a [Add a score]: ");
                      array[count] = in.nextInt();
                      myQuiz.add(array[count]);
                      counter++;
                  break;

你必须在你之前的switch-case语句之外定义myQuiz更具体。