java-创建字符串或整数条件
构建调查应用程序时,我遇到了这个问题的障碍。我不明白我的问题到底在哪里,但当我试图创建一个双选项菜单时,它不允许我编译或运行它 错误:java-创建字符串或整数条件,java,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: e
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()
{
}
}
package调查;
导入java.util.Scanner;
导入java.io.Serializable;
公共类调查实现了可序列化
{
私有字符串surveyName;
私人问题[]问题;
私有整数;
私有int-maxResponses;
私有布尔初始化;
公众调查(第n组)
{
surveyName=n;
初始化=假;
}
//initialize()设置调查的numQuestions、MaxResponses和问题
公共问题
{
扫描仪输入=新扫描仪(System.in);
System.out.println(“初始化调查\”+surveyName+“\”\n”);
//添加密码验证方法!?!?是!!!请参阅银行帐户实验室
System.out.print(“输入最大响应数:”);
maxResponses=input.nextInt();
System.out.print(“输入问题数量:”);
numQuestions=input.nextInt();
input.nextLine();//必须这样做才能“吃掉”新行字符,否则下一个输入将无法正常工作
System.out.println();
问题=新问题[numQuestions];
for(int i=0;i
您正在循环外使用i
。因为您在for
循环中声明了i
,所以i
的范围仅为循环。循环一结束,它就不存在了
来自编译器的错误消息告诉您错误在哪一行代码上,以及错误的确切内容。阅读这些内容是值得的。您使用的是循环外的
i
。因为您在for
循环中声明了i
,所以i
的范围仅为循环。循环一结束,它就不存在了
来自编译器的错误消息告诉您错误在哪一行代码上,以及错误的确切内容。阅读这些内容是值得学习的。你到底犯了什么错误?我更新了我的帖子。请再次检查。首先搜索错误本身以查找有用的信息,包括。您得到的确切错误是什么?我更新了我的帖子。请再次检查。首先搜索错误本身以查找有用的信息,包括。