为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

开心每一天1111 2024-01-28 15:57:27
我试图在每次用户输入“是”时添加 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 + "%");        }    }
查看完整描述

3 回答

?
慕工程0101907

TA贡献1887条经验 获得超5个赞

这里的问题是,对于每个受访者,您都将他们的答案记录在问题编号decisions[j]中;j但稍后您可以通过迭代受访者编号来计算“是”响应的decisions[k]数量k

要么decisions[i]表示某位受访者对问题 的回答i,要么表示i第 3 位受访者对问题 1 的回答。它不能同时表示两者。您需要重新考虑如何存储这些数据。

此外,由于decisions[j]是为每个受访者编写的j,因此每次都会覆盖该数组,这意味着您最终只会存储最后一个受访者的结果。

二维数组可能是一个很好的解决方案,其中decisions[i][j]表示i第 3 个受访者对问题 的回答j


查看完整回答
反对 回复 2024-01-28
?
阿波罗的战车

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();

    }

}


查看完整回答
反对 回复 2024-01-28
?
Helenr

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();



        }

}


查看完整回答
反对 回复 2024-01-28
  • 3 回答
  • 0 关注
  • 95 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信