我试图在用户每次输入“是”时添加25,然后打印结果,但是在将它们相加时,它始终缺少第一个响应。因此,如果我为乳制品键入“是”,我只会得到75%的收益?对于较大的代码,这是一项正在进行的工作,但是基本上,如果您现在对乳制品键入“是”,则应该将它们加起来等于100。
尝试了许多不同的选项,却无处可寻
import java.util.Arrays;
import java.util.Scanner;
public class question4 {
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];
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[j]=userTypes.nextLine();
}
System.out.println(Arrays.toString(decisions));
}
System.out.println("Allergy Results");
for (int k=0; k <= respondents.length - 1; k++ ){
if (decisions[k].equals("Yes")) {
dairy= dairy + 25;
}
}
System.out.println("Dairy Allergy Results = " + dairy + "%");
}
}
答案 0 :(得分:2)
这里的问题是,对于每个受访者,您都将他们的答案记录在decisions[j]
中,其中j
是问题编号;但是随后您通过在decisions[k]
上进行迭代来计算“是”的响应数,其中k
是响应者数。
decisions[i]
表示某个受访者对问题i
的回答,或者表示第i
个受访者对问题1的回答。您需要重新考虑如何存储这些数据。
此外,由于正在为每个受访者的每个decisions[j]
编写j
,因此每次都会覆盖该数组,这意味着您最终只会存储最后一个受访者的结果。
二维数组可能是一个很好的解决方案,其中decisions[i][j]
表示第i
位受访者对问题j
的回答。
答案 1 :(得分:0)
首先,让我们格式化代码。为了存储3个三个不同问题的4个不同用户的决策,您需要像这样的数组(数据结构)。另外,看起来您仅对(现在)对乳品问题的决定感兴趣。因此,只需在计算中检查一下即可。我已经更新了代码并添加了注释。需要更新存储结果的部分以及如何计算乳制品的总量。
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();
}
}
答案 2 :(得分:0)
好吧,最后我真正的基本解决方案是以下方法,虽然不理想,但是我是一个初学者:)
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();
}
}