2017-05-30 59 views
0

构建一个调查应用程序,我遇到了这个问题的障碍。我不明白我的问题在哪里,但是当我尝试创建两个选项菜单时,它不允许我编译或运行它。java - 创建一个字符串或整数条件

错误:

Compiling 2 source files to C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\build\classes 
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\src\survey\Survey.java:71: error: cannot find symbol 
      System.out.print("Enter text for question " + (i+1) + ": "); 
    symbol: variable i 
    location: class Survey 
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\src\survey\Survey.java:75: error: cannot find symbol 
      questions[i] = new IntegerQuestion(input.nextLine(),maxResponses); 
    symbol: variable i 
    location: class Survey 
2 errors 
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:930: The following error occurred while executing this line: 
C:\Users\martin.shaba\Documents\NetBeansProjects\Survey\nbproject\build-impl.xml:270: Compile failed; see the compiler error output for details. 
BUILD FAILED (total time: 1 second) 

以下是我希望它是...

选择从以下选项: 的S - 创建在一个问题 - 在一个字符串 N新建一个问题整数

我当前的代码:

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]; 

      for(int i = 0; i < numQuestions;i++) 
      { 
       char choice; 

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


      //loop until a valid input is entered 

       System.out.print("Enter choice: "); 
       choice = input.next().charAt(0); 
       //if choice is one of the options, return it. Otherwise keep looping 
       if(choice == 'S' || choice == 'N' ) 
        return choice; 
       else 
       { 
        System.out.println("Invalid choice. Ensure a capital letter. Please re-enter."); 
        choice = '?'; 
       } 
      (choice == '?'); 

      return choice; //will never get here, but required to have a return statement to compile 
     } 
       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; 
     } 


     /* 
      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() 
     { 

     } 


    } 
+0

你准确得到了什么错误? – Mureinik

+0

我更新了我的帖子。请再检查一次。 –

+0

首先搜索错误本身以找到有用的信息,包括[this great link](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean)。 –

回答

2

您正在循环之外使用i。由于您在for循环中声明了i,因此i的范围仅为循环。只要循环结束,它就不再存在。

来自编译器的错误消息告诉你错误出现在哪一行代码,以及错误是什么。阅读这些内容值得学习。

相关问题