亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我試圖在每次用戶輸入“是”時添加 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條經驗 獲得超4個贊

好吧,最后我真正基本的解決方案如下,并不理想,但嘿我是初學者:)


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 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號