简体   繁体   中英

I'm trying to send arrays between 2 sheets in Java and toString

Cannot make a reference to the nonstatic method toString() other is saying that I can't have a static toString but when I change it and go to the main it says I need a static toString. I was wondering if anyone could make sense of it because I referenced multiple codes and notes from classes but could not understand.

    import java.util.Scanner;
    
    import java.util.Arrays;
    
    class Main 
      {
         public static void main(String[] args) 
         {
           String[] answer = new String();
           answerArray();
           Quiz Quiz1 = new Quiz();
           Quiz.toString();
           System.out.print("Number of Correct Answers: ");
           System.out.println(Quiz.totalCorrect(answerArray[]));
           System.out.print("Number of Mistakes: ");
           System.out.println(Quiz.totalMistakes(answerArray[]));
           System.out.println("The Student is"+Quiz.isPassing());
         }
        
        public static String answerArray()
       {
         Scanner scan = new Scanner(System.in);
         String[] answerArray = new String[10];
         
         for (int i = 0; i < answerArray.length; i++)
         {
          System.out.println("Enter Grade "+i+1);
          answerArray[i]=scan.next;
          answerArray[i]=answerArray[i].toLowerCase();
         }
         return answerArray;
       }
    }

Quiz Class:

 import java.util.Arrays;

class Quiz 
  {
  
   public static void main(String[] args) {
     
     String[] answerKey = new String[10];
     String[] studentAns = new String[answerKey.length];
     
     answerKey[0] = B;
     answerKey[1] = D;
     answerKey[2] = C;
     answerKey[3] = A;
     answerKey[4] = E;
     answerKey[5] = A;
     answerKey[6] = B;
     answerKey[7] = A;
     answerKey[8] = E;
     answerKey[9] = B;
   
  }

   public static void studentAns(String answerKey[])
   {
    for (int i = 0; i < answerArray.length; i++)
    studentAns[i]=answerKey[i];
    for (int i=0; i<answerKey.length;i++)
    studentAns[i]=studentAns[i].toUpperCase();
   }

   public static String getstudentAns()
   {
    return studentAns;
   }

   public static int totalCorrect(String answerArray[])
   {
    int numCorrect=0;
    for (int i = 0; i < answerArray.length; i++)
     {
       if (answerArray[i].equals(studentAns[i]))
       numCorrect=numCorrect+1;
     }
     return numCorrect;
   }
   
   public static int totalMistakes(String answerArray[])
   {
    int numMistakes=0;
    for (int i = 0; i < answerKey.length; i++)
      {
      if (answerArray[i]!=studentAns[i])
      numMistakes=numMistakes+1;
      }
    return numMistakes;
   }

   public static boolean isPassing()
   {
    if (totalCorrect-numMistakes>=7)
    return PASSED;
    else
    return FAILED;
   }

   public static String toString(String answerArray[])
   {
     String firstLine = "ANSWER KEY \t Student's Answers";
     for (int i = 0; i < answerKey.length; i++)
     {
       String full=(i+1)+") "+answerArray[i]+"\t\t"+(i+1)+studentAns[i]+"\n";
       return full;     
     }
   }
}
                                                      

Here you are trying to use a static method toString() but that does not exist because classes only have the nonstatic method toString().

Quiz.toString();

To use your static method you should write this instead:

Quiz.toString(answerArray());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM