我需要一些帮助。我试图让这个阵列正常工作,但不知道我做错了什么。我是java的菜鸟,真的需要一些帮助
private static int respondentID;
private static int count;
static void enterQuestions() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
private String surveyName;
private boolean surveyStart = true;
private boolean surveyStop = true;
private String question [] = new String[10];
private int responses[][]= new int[10][10];
Scanner input = new Scanner(System.in);
// first overloaded constructor
public Phase2() {
this( "Customer Survey" );
respondentID = 0;
count = 0;
} // end
// second overloaded constructor
public Phase2( String title ) {
surveyName = title;
respondentID = 0;
count = 0;
} // end constructor Survey
// method to be called when a user starts filling out a survey ( surveyStart will have been set to "true" )
public int startSurveyCount( int ct ) { // parameter for testing only
if( surveyStart ) {
if( respondentID > 0 ) {
if( count >= respondentID ) {
count++;
} else {
setCount( getRespondentID() );
}
} else {
//test
setCount( ct );
count = getCount();
count++;
setCount( count - 1 );
}
}
return count;
} // end method
// method to be called when a survey is successfully
public int generateRespondentID() {
if( surveyStop ) {
//
count = getCount();
setRespondentID( count );
} else {
if( count < 2 ) {
count = 0;
respondentID = 0;
} else {
count--;
setCount( count );
}
}
return respondentID;
} // end method generateRespondentID
public void setRespondentID( int count ) {
// count is the number of completed surveys.
respondentID = count;
respondentID++; // and then incremented by 1.
} //end method
public int getRespondentID() {
return respondentID;
} // end method
public void setSurveyTitle( String title ) {
surveyName = title;
} // end method
public String getSurveyTitle() {
return surveyName;
} // end method
public void setCount( int ct ) {
count = ct;
} // end method
public int getCount() {
return count;
} // end method
public void setSurveyStart( boolean surveySt ) {
surveyStart = surveySt;
} // end method
public boolean getSurveyStart() {
return surveyStart;
} // end method
public void setSurveySubmit( boolean surveySub ) {
surveyStop = surveySub;
} // end method
public boolean getSurveySubmit() {
return surveyStop;
} // end method
public void logResponse(int respondentID, int questionNumber, int responseEntered)
{
responses[respondentID] [questionNumber-1] = responseEntered;
}
public void displaySurveyResults (int no)
{
for (int j=0; j<10; j++)
System.out.print("Question"+(no)+" : " + question[no-1]+"Reply");
if (responses[respondentID][no] == 0)
{
System.out.print("NO");
}
else
{
System.out.print("Yes");
}
}
public void enterQuestion()
{
for (int i=0; i<10; i++)
{
System.out.println("Enter Question "+(i+1)+" : ");
question[i] = input.nextLine();
}
}
public void displayQuestionStats(int no)
{
int answer;
System.out.print("Question"+(no)+" : "+question[no-1]+" (0-No/1-Yes) : ");
answer = input.nextInt();
logResponse(respondentID, no, answer);
}
}
这是我的测试人员
public static void main(String[] args) {
Scanner input = new Scanner( System.in );
System.out.println( "Below are the results of running the no–argument");
// demonstrates the no–arg constructor
Phase2 noArgSurvey = new Phase2();
System.out.printf( "The no–argument survey values are:\ntitle: %s\n"
+ "initial value of respondentID: %d\ncount: %d\n",
noArgSurvey.getSurveyTitle(), noArgSurvey.getRespondentID(),
noArgSurvey.getCount() );
// demonstrates the constructor with a title argument ( for user input of survey title )
System.out.println( "\nPlease enter a name for the survey" );
String inputTitle = input.nextLine();
System.out.println(); // inserts a blank line
Phase2 titleArgConst = new Phase2( inputTitle );
System.out.printf( "Survey Name is: %s\n"
+ "initial values of:\nrespondentID: %d\ncount: %d\n\n",
titleArgConst.getSurveyTitle(), titleArgConst.getRespondentID(),
titleArgConst.getCount() );
//respondent id test
System.out.println( "This will test the generateRespondentID method.\n\n"
+ "Enter the number of surveys that have been taken");
int testInt = input.nextInt();
// values for respondentID and count after 1 survey has been successfully submitted
System.out.println( "\nAssuming " + testInt + " surveys submitted");
Phase2 oneDone = new Phase2();
oneDone.startSurveyCount( testInt );
oneDone.generateRespondentID();
System.out.printf( "The Respondent ID is: %d\ncount: %d\n\n",
oneDone.getRespondentID(), oneDone.getCount() );
noArgSurvey.enterQuestion();
for(int i = 1; i <= 10; i++)
{
noArgSurvey.displayQuestionStats(i);
}
//Display The Inputs Entered by User
System.out.println("Result for Survey with Title \""+titleArgConst.getSurveyTitle()+"\" :");
for(int i=1; i<11; i++)
{
noArgSurvey.displaySurveyResults(i);
}
} // end main method
} // end class SurveyTest
答案 0 :(得分:1)
将循环条件更改为
for(int i = 1; i < 10; i++)
答案 1 :(得分:0)
Java遵循零索引。因此,在您的情况下,question
和responses
数组的大小为10
,这意味着它将从0 to 9
使用for(int i = 1; i < 10; i++)
进行迭代而不是迭代。或使用arrayName.length
length
是由JVM
提供的变量,它在运行时为您提供size
数组。
答案 2 :(得分:0)
AS Vishrant提到* Java遵循零索引。所以在你的情况下,问题和响应数组的大小为10,这意味着它将从0迭代到9 *
在你的测试者类中,你试图在2个地方循环访问第10个索引for(int i = 1; i <= 10; i++) (1)
{
noArgSurvey.displayQuestionStats(i);
}
for(int i=1; i<11; i++) (2)
{
noArgSurvey.displaySurveyResults(i);
}
你应该写
for(int i = 0; i < 10; i++) (1)
{
noArgSurvey.displayQuestionStats(i);
}
for(int i=0; i<10; i++) (2)
{
noArgSurvey.displaySurveyResults(i);
}
编辑添加
public void displaySurveyResults (int no)
{
for (int j=0; j<10; j++)
System.out.print("Question"+(no)+" : " + question[no]+"Reply"); <<<--------- change [no-1] to [no]
if (responses[respondentID][no] == 0)
{
System.out.print("NO");
}