2017-05-31 44 views
-3

我需要位於Survey文件末尾的reportSummary()方法的幫助。我正在使用我在網上找到的說明從頭開始制定調查計劃。我一直在這一點上,但我不知道我錯過了什麼。java - 顯示報告和處理結果

電流誤差(使用幫助從下面的答案後):

C:\Users\Nael\Documents\NetBeansProjects\Survey\src\survey\Survey.java:156: error: illegal start of type 
    if (questions[i] instanceof (DoubleQuestions)) 
C:\Users\Nael\Documents\NetBeansProjects\Survey\src\survey\Survey.java:156: error: not a statement 
    if (questions[i] instanceof (DoubleQuestions)) 
C:\Users\Nael\Documents\NetBeansProjects\Survey\src\survey\Survey.java:156: error: ';' expected 
    if (questions[i] instanceof (DoubleQuestions)) 
C:\Users\Nael\Documents\NetBeansProjects\Survey\src\survey\Survey.java:186: error: class, interface, or enum expected 
} 
4 errors 
C:\Users\Nael\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:930: The following error occurred while executing this line: 
C:\Users\Nael\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details. 

Survey.java:

package survey; 

import java.util.Scanner; 
import java.io.Serializable; 


public class Survey implements Serializable 
{ 
    private String surveyName; 
    private Question[] questions; 
    private int numQuestions; 
    private int maxResponses; 
    private boolean initialized; 

    public Survey(String n) 
    { 
     surveyName = n; 
     initialized = false; 
    } 


     //initialize() sets up the numQuestions, MaxResponses, and questions for the survey 

    public char Questions() 
    { 
     Scanner input = new Scanner(System.in); 

     System.out.println("Initializing survey \"" + surveyName + "\"\n"); 

     //add a method for password validation!?!?!? yes!!! see the bank accounts lab 

     System.out.print("Enter max number of responses: "); 
     maxResponses = input.nextInt(); 

     System.out.print("Enter number of questions: "); 
     numQuestions = input.nextInt(); 

     input.nextLine(); //have to do this to "eat" the new line character or the next input won't work correctly 
     System.out.println(); 

     questions = new Question[numQuestions]; 
      char choice='c'; 
     for(int i = 0; i < numQuestions;i++) 
     { 


     //output menu options 
     System.out.println();  
     System.out.println(" S - Create a String Question"); 
     System.out.println(" N - Create a Integer Question"); 
     System.out.println(" D - Create a Double Question"); 


     //loop until a valid input is entered 

      System.out.print("Enter choice: "); 
      choice = input.next().charAt(0); 
      input.nextLine(); //still have to "eat" the current response 

      //if choice is one of the options, return it. Otherwise keep looping 
      if(choice == 'N' ) 
      { 
       System.out.print("Enter text for question " + (i+1) + ": "); 


      //you will also need to ask what KIND of question - right now, defaults to integer question 

      questions[i] = new IntegerQuestion(input.nextLine(),maxResponses); 
      initialized = true; 
      } 

      else if(choice == 'S') 
      { 
        System.out.print("Enter text for question " + (i+1) + ": "); 

      //you will also need to ask what KIND of question - right now, defaults to integer question 

      questions[i] = new TextQuestion(input.nextLine(),maxResponses); 
      initialized = true; 
      } 
      else if(choice == 'D') 
      { 
        System.out.print("Enter text for question " + (i+1) + ": "); 

      //you will also need to ask what KIND of question - right now, defaults to integer question 

      questions[i] = new DoubleQuestions(input.nextDouble(),maxResponses); 
      initialized = true; 
      } 
      else 
      { 
       System.out.println("Invalid choice. Ensure a capital letter. Please re-enter."); 
       choice = '?'; 
      } 

     } 

         return choice; 

    } 


    /* 
     run() gives the survey to a new survey taker, basically asks all the questions in the survey 
    */ 

    public void startSurvey() 
    { 
     if(initialized) 
     { 
      System.out.println("Welcome to the survey \"" + surveyName + "\"\n"); 

      for(int i = 0;i < numQuestions; i ++) 
      { 
       questions[i].askQuestion(); 
      } 

      System.out.println("Thank you for participating!"); 
     } 
     else 
     { 
      System.out.println("Survey has not yet been setup. Please initialize first."); 
     } 

    } 

    /* 
     displayResults() displays the raw data for the survey 
    */ 
    public void Results() 
    { 
     System.out.println("Displaying data results for \"" + surveyName + "\"\n"); 

     for(int i = 0;i < numQuestions; i ++) 
     { 
      questions[i].displayResults(); 
      System.out.println(); 
     } 
    } 

    /* 
     displayReportSummary() should run tests on your data 
     Examples could be: the most common response (median), the average response (mean), or display a graph of the results? 
     The choices are endless! 
    */ 
    public void reportSummary() 
    { 
     for(int i=0;< numQuestions; i ++) 
     { 
      if (questions[i] instanceof (DoubleQuestions) 
        { 
         DoubleQuestions temp = (DoubleQuestions) questions[i]; 
         temp.doubleAverage(); 
        System.out.println(); 
        } 
     } 
    } 


} 

DoubleQuestion:

package survey; 


import java.util.Scanner; 


public class DoubleQuestions extends Question 


{ 
     private double[] responses; 


    public DoubleQuestions(double q, int m) 
     { 
      super(Double.toString(q),(m)); 
      responses = new double[m]; 
     } 

     @Override 
     public void askQuestion() 
     { 
      double response;    
      Scanner input = new Scanner(System.in); 
      System.out.print(question + " "); 
      input.nextLine(); //still have to "eat" the current response   
      response = input.nextDouble(); 
      responses[numResponses] = response; 
      numResponses++; 
     } 

     @Override 
     public void displayResults() 
     { 
      System.out.println(question); 
      for(int i = 0; i < numResponses;i++) 
       System.out.println(responses[i]); 
     } 

} 
+6

剛纔你的問題是什麼?另外,請刪除不需要的代碼並創建一個[MCVE] – anacron

+0

我的問題是在'reportSummary()'我的代碼不起作用。 –

+0

不是功能意味着什麼? – Blasanka

回答

0

在這個循環中有三個錯誤。

一個在這條線:

for(int i=0;< numQuestions; i ++) 

在這裏,您misswd條件< numQuestions

另一位在這一行:

if (questions[i] instanceof (DoubleQuestions) 

在這裏,你錯過了密切的支架)

if (questions[i] instanceof (DoubleQuestions)) 

上面的行應該是:

if (questions[i] instanceof DoubleQuestions) 

修復:

for(int i=0; i < numQuestions; i ++) 
{ 
    if (questions[i] instanceof DoubleQuestions) 
      { 
       DoubleQuestions temp = (DoubleQuestions) questions[i]; 
       temp.doubleAverage(); 
      System.out.println(); 
      } 
} 

而且你doubleAverage()方法在DoubleQuestions類沒有實現。所以你不能引用它。

所以你不能這樣做:

DoubleQuestions temp = (DoubleQuestions) questions[i]; 
temp.doubleAverage(); 

首先實現doubleAverage()。然後嘗試一下。

0

你應該嘗試和理解錯誤編譯器向您投擲的消息。它清楚地說明了行號和你在代碼中做了什麼錯誤。

您需要關閉,如果條件的括號:

if (questions[i] instanceof (DoubleQuestions) 

必須

if (questions[i] instanceof (DoubleQuestions)) 

for循環在條件缺少i

for(int i=0;< numQuestions; i ++) 

必須是:

for(int i=0;i < numQuestions; i ++) 

public void reportSummary() 
{ 
    for(int i=0;< numQuestions; i ++) 
    { 
     if (questions[i] instanceof (DoubleQuestions)) // This line 
       { 
        DoubleQuestions temp = (DoubleQuestions) questions[i]; 
        temp.doubleAverage(); 
       System.out.println(); 
       } 
    } 
} 

希望這有助於!

+0

謝謝你的回答,它確實修復了錯誤,但它現在有7個錯誤,請再次檢查代碼,我更新了它並在新錯誤上標記了你。 –