3 回答
TA贡献1887条经验 获得超5个赞
这里的问题是,对于每个受访者,您都将他们的答案记录在问题编号decisions[j]
中;j
但稍后您可以通过迭代受访者编号来计算“是”响应的decisions[k]
数量k
。
要么decisions[i]
表示某位受访者对问题 的回答i
,要么表示i
第 3 位受访者对问题 1 的回答。它不能同时表示两者。您需要重新考虑如何存储这些数据。
此外,由于decisions[j]
是为每个受访者编写的j
,因此每次都会覆盖该数组,这意味着您最终只会存储最后一个受访者的结果。
二维数组可能是一个很好的解决方案,其中decisions[i][j]
表示i
第 3 个受访者对问题 的回答j
。
TA贡献1862条经验 获得超6个赞
首先,让我们格式化代码。为了存储 4 个不同用户对 3 个不同问题的决定,您需要您的数组(数据结构)是这样的。另外,看起来您(目前)只对有关乳制品问题的决定感兴趣。因此,只需在计算中检查一下即可。我已经更新了代码并添加了注释。需要更新存储结果的部分以及计算乳制品总数的方式。
import java.util.Arrays;
import java.util.Scanner;
public class Question {
public static void main(String[] args) {
Scanner userTypes = new Scanner(System.in); //new object for user input
String[] respondents = {"Cormac", "Orla", "Paul", "Sarah"};
String[] questions = {"Are you allergic to Dairy?", "Are you allergic to nuts?", "Are you gluten intolerent?"};
String[][] decisions = new String [4][3];//You have 4 respondents and You have 3 questions
int dairy= 0;
int nuts= 0;
int gluten=0;
for (int i=0; i<= respondents.length -1 ;i++) {
System.out.println(respondents[i]);
for (int j=0; j<= questions.length -1; j++) {
System.out.println(questions[j]);
decisions[i][j]=userTypes.nextLine();
}
System.out.println("Decisions :: "+Arrays.toString(decisions[i]));//Need to print the result per user
}
System.out.println("Allergy Results");//If you are only interested in dairy decision for the 4 user
for (int k=0; k <= respondents.length - 1; k++ ){
if (decisions[k][0].equals("Yes")) {//for all user check the first decision (as your first question is about dairy)
dairy= dairy + 25;
}
}
System.out.println("Dairy Allergy Results = " + dairy + "%");
userTypes.close();
}
}
TA贡献1780条经验 获得超3个赞
好吧,最后我真正基本的解决方案如下,并不理想,但嘿我是初学者:)
public class question4 {
static void allergyTest() { //method for the allergy test
Scanner userTypes = new Scanner(System.in); //new object for user input
String[] respondents = {"Cormac", "Orla", "Paul", "Sarah"};//string array that contains the name of the people being surveyed
String[] questions = {"Are you allergic to Dairy?", "Are you allergic to nuts?", "Are you gluten intolerent?"};// string array to store the questions
String[] decisions = new String [3];//string array to store the responses to the questions
int dairy= 0; //int to store dairy percentage
int nuts= 0;// int to store nuts percentage
int gluten=0; //int to store gluten percentage
for (int i=0; i<= respondents.length -1 ;i++) { // for loop to go through each respondent
System.out.println(respondents[i]); //print their name
for (int j=0; j<= questions.length -1; j++) { //then a for loop to loop through the questions for each respondent
System.out.println(questions[j]); //print the actual question
decisions[j]=userTypes.nextLine(); //take the users input
while(!decisions[j].equals("yes")&& !decisions[j].equals("no")) { //check if the users input is valid, as in yes or no
System.out.println("please type yes or no as your answer"); //if not tell them to type it correctly
decisions[j]=userTypes.nextLine(); //store the yes or no once correctly typed
}
}
if (decisions[0].equals("yes")) { //add up the yes
dairy = dairy +25; //lazy way of getting a percentage because I know the amount of respondents & answers
}
if (decisions[1].equals("yes")) {
nuts = nuts +25;
}
if (decisions[2].equals("yes")) {
gluten = gluten +25;
}
}
System.out.println("Allergy Results");// print the results below
System.out.println("Percentage of people allergic to dairy= "+ dairy +"%");
System.out.println("Percentage of people allergic to nuts= "+ nuts +"%");
System.out.println("People who think they are allergic to gluten= "+ gluten +"%");
}
public static void main(String[] args) { //call the allergy test
allergyTest();
}
}
添加回答
举报