2017-05-30 10 views
0

調査アプリケーションを構築していますが、この問題が発生しているロードブロッキングに遭遇しています。私は問題がどこにあるのかを理解していませんが、2つのオプションメニューを作成しようとしているときに、コンパイルや実行を許可していません。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

[このすばらしいリンク](https://stackoverflow.com/questions/25706216/what-does-a-cannot-find-symbol-compilation-error-mean)などの有用な情報を見つけるために、まずエラーそのものを検索してください。 –

答えて

2

ループ外でiを使用しています。 forループでiを宣言したため、iの範囲はループのみです。ループが終了するとすぐに存在しなくなります。

コンパイラからのエラーメッセージは、エラーのコード行とエラーの内容を正確に伝えます。これらを読むことを学ぶ価値があります。

関連する問題