java-显示报告和流程结果
我需要有关位于调查文件末尾的java-显示报告和流程结果,java,Java,我需要有关位于调查文件末尾的reportSummary()方法的帮助。我正在使用我在网上找到的说明从头开始构建一个调查程序。我一直到这一点,但我不确定我错过了什么 当前错误(使用以下答案的帮助后): Survey.java: package survey; import java.util.Scanner; import java.io.Serializable; public class Survey implements Serializable { private S
reportSummary()
方法的帮助。我正在使用我在网上找到的说明从头开始构建一个调查程序。我一直到这一点,但我不确定我错过了什么
当前错误(使用以下答案的帮助后):
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();
}
}
}
}
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];
char choice='c';
for(int i=0;i
双重问题:
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]);
}
}
package调查;
导入java.util.Scanner;
公共类双重问题扩展了这一问题
{
私人双[]反应;
公开双题(双q,整数m)
{
超(双toString(q)、(m));
响应=新的双[m];
}
@凌驾
公众提问
{
双重反应;
扫描仪输入=新扫描仪(System.in);
系统输出打印(问题+“”);
input.nextLine();//仍然必须“吃掉”当前响应
response=input.nextDouble();
响应[numResponses]=响应;
numResponses++;
}
@凌驾
public void displayResults()
{
System.out.println(问题);
for(int i=0;i
您应该尝试理解编译器向您抛出的错误消息
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]);
}
}
if (questions[i] instanceof (DoubleQuestions)
if (questions[i] instanceof (DoubleQuestions))
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();
}
}
}
for(int i=0;< numQuestions; i ++)
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();
}
}
DoubleQuestions temp = (DoubleQuestions) questions[i];
temp.doubleAverage();