我试图在每次用户输入“是”时添加 25,然后打印结果,但在添加它们时始终缺少第一个响应

我试图在每次用户输入“是”时添加 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 + "%");

        }    

}


开心每一天1111
浏览 96回答 3
3回答

慕工程0101907

这里的问题是,对于每个受访者,您都将他们的答案记录在问题编号decisions[j]中;j但稍后您可以通过迭代受访者编号来计算“是”响应的decisions[k]数量k。要么decisions[i]表示某位受访者对问题 的回答i,要么表示i第 3 位受访者对问题 1 的回答。它不能同时表示两者。您需要重新考虑如何存储这些数据。此外,由于decisions[j]是为每个受访者编写的j,因此每次都会覆盖该数组,这意味着您最终只会存储最后一个受访者的结果。二维数组可能是一个很好的解决方案,其中decisions[i][j]表示i第 3 个受访者对问题 的回答j。

阿波罗的战车

首先,让我们格式化代码。为了存储 4 个不同用户对 3 个不同问题的决定,您需要您的数组(数据结构)是这样的。另外,看起来您(目前)只对有关乳制品问题的决定感兴趣。因此,只需在计算中检查一下即可。我已经更新了代码并添加了注释。需要更新存储结果的部分以及计算乳制品总数的方式。import java.util.Arrays;import java.util.Scanner;public class Question {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Scanner userTypes = new Scanner(System.in); //new object for user input&nbsp; &nbsp; &nbsp; &nbsp; String[] respondents = {"Cormac", "Orla", "Paul", "Sarah"};&nbsp; &nbsp; &nbsp; &nbsp; String[] questions = {"Are you allergic to Dairy?", "Are you allergic to nuts?", "Are you gluten intolerent?"};&nbsp; &nbsp; &nbsp; &nbsp; String[][] decisions = new String [4][3];//You have 4 respondents and You have 3 questions&nbsp; &nbsp; &nbsp; &nbsp; int dairy= 0;&nbsp; &nbsp; &nbsp; &nbsp; int nuts= 0;&nbsp; &nbsp; &nbsp; &nbsp; int gluten=0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<= respondents.length -1 ;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(respondents[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j=0; j<= questions.length -1; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(questions[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decisions[i][j]=userTypes.nextLine();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Decisions :: "+Arrays.toString(decisions[i]));//Need to print the result per user&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Allergy Results");//If you are only interested in dairy decision for the 4 user&nbsp; &nbsp; &nbsp; &nbsp; for (int k=0; k <= respondents.length - 1; k++ ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (decisions[k][0].equals("Yes")) {//for all user check the first decision (as your first question is about dairy)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dairy= dairy + 25;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Dairy Allergy Results = " + dairy + "%");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; userTypes.close();&nbsp; &nbsp; }}

Helenr

好吧,最后我真正基本的解决方案如下,并不理想,但嘿我是初学者:)public class question4 {&nbsp; &nbsp; &nbsp; &nbsp; static void allergyTest() { //method for the allergy test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Scanner userTypes = new Scanner(System.in); //new object for user input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] respondents = {"Cormac", "Orla", "Paul", "Sarah"};//string array that contains the name of the people being surveyed&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] questions = {"Are you allergic to Dairy?", "Are you allergic to nuts?", "Are you gluten intolerent?"};// string array to store the questions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] decisions = new String [3];//string array to store the responses to the questions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int dairy= 0; //int to store dairy percentage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int nuts= 0;// int to store nuts percentage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int gluten=0; //int to store gluten percentage&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<= respondents.length -1 ;i++) { // for loop to go through each respondent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(respondents[i]); //print their name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j=0; j<= questions.length -1; j++) { //then a for loop to loop through the questions for each respondent&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(questions[j]); //print the actual question&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decisions[j]=userTypes.nextLine(); //take the users input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while(!decisions[j].equals("yes")&& !decisions[j].equals("no")) { //check if the users input is valid, as in yes or no&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("please type yes or no as your answer"); //if not tell them to type it correctly&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; decisions[j]=userTypes.nextLine(); //store the yes or no once correctly typed&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (decisions[0].equals("yes")) { //add up the yes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dairy = dairy +25; //lazy way of getting a percentage because I know the amount of respondents & answers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (decisions[1].equals("yes")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nuts = nuts +25;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (decisions[2].equals("yes")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gluten = gluten +25;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Allergy Results");// print the results below&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Percentage of people allergic to dairy= "+ dairy +"%");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Percentage of people allergic to nuts= "+ nuts +"%");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("People who think they are allergic to gluten= "+ gluten +"%");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] args) { //call the allergy test&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; allergyTest();&nbsp; &nbsp; &nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java