gpt4 book ai didi

java - 我需要形成一个循环来读取调查问题的答案并将其存储在二维数组中

转载 作者:行者123 更新时间:2023-12-02 08:39:27 26 4
gpt4 key购买 nike

我的作业要求:

  • 用于保存受访者结果的二维数组
  • 带有 int 参数的 displaySurveyResults() 方法
  • 可容纳 10 个问题的字符串数组
  • enterQuestions() 方法
  • 具有 3 个参数的 logResponse() 方法

到目前为止,我已经创建了一个一维数组来保存问题和一个 lopp 来读取它们。我不知道如何使用这些问题来将结果存储到二维数组中。到目前为止我的代码

public class Survey {

private String surveyTitle;

public String getSurveyTitle(){
return surveyTitle;
}
static int respondantID=0;
static int generateRespondantID(){
return ++respondantID;
}
public Survey(String surveyTitle){
this.surveyTitle = surveyTitle;
}
public Survey(){
surveyTitle = "Customer Survey";
}


public static void main(String[] args) {

Survey s = new Survey();
System.out.println(s.getSurveyTitle());
System.out.println(Survey.generateRespondantID());
System.out.println();

//Array to Hold Questions
String[] questions = new String[10]; // declare array
questions[0] = "Question 1";
questions[1] = "Question 2";
questions[2] = "Question 3";
questions[3] = "Question 4";
questions[4] = "Question 5";
questions[5] = "Question 6";
questions[6] = "Question 7";
questions[7] = "Question 8";
questions[8] = "Question 9";
questions[9] = "Question 10";

Scanner sc = new Scanner(System.in);

//loop to read in questions
for(int index=0; index<10; index++){
System.out.println("Please enter a question");
questions[index] = sc.nextLine();
System.out.println("Question: " + questions[index]);
}

//Array to hold results of questions
int[][] answers = new int[10][10];

//loop to read in answers to questions
for(int index=0; index<10; index++){
System.out.println([index]);
}

最佳答案

您可以使用二维字符串数组来存储此数据

第 1 列将存储问题,其他列将存储人们的回答

如果您有 5 个问题并且有 3 个人回答问题,那么您将获得字符串[5][3]

您可以运行下面的程序来存储数据

public static void main(String[] args) {

int sizeOfQuestions = 0;
int sizeOfRespondants = 0;

Scanner sc = new Scanner(System.in);
System.out.println("Enter the total number of questions");
sizeOfQuestions = sc.nextInt();
System.out.println("Enter the total number of respondants");
sizeOfRespondants = sc.nextInt();

String[][] surveyPool = new String[sizeOfQuestions][sizeOfRespondants];


for (int i = 0; i < sizeOfQuestions; i++){
System.out.println("Enter question " + (i + 1));
surveyPool[i][0] = sc.nextLine();
}

System.out.println("Please enter the answers : \n");

for (int i = 0; i < sizeOfQuestions; i++){
System.out.println("Question" + surveyPool[i][0]);
for (int j = 0; j < sizeOfRespondants; j++){
System.out.println("Answer by " + (j + 1));
surveyPool[i][j] = sc.nextLine();
}
}
}

关于java - 我需要形成一个循环来读取调查问题的答案并将其存储在二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61469517/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com