第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我試圖在每次用戶輸入“是”時(shí)添加 25,然后打印結(jié)果,但在添加它們時(shí)始終缺少第一個(gè)響應(yīng)

我試圖在每次用戶輸入“是”時(shí)添加 25,然后打印結(jié)果,但在添加它們時(shí)始終缺少第一個(gè)響應(yīng)

開心每一天1111 2024-01-28 15:57:27
我試圖在每次用戶輸入“是”時(shí)添加 25,然后打印結(jié)果,但在添加它們時(shí)它總是缺少第一個(gè)響應(yīng)。那么,如果我對(duì)乳制品輸入“是”,我只能得到 75% 的結(jié)果?這是針對(duì)較大代碼段的一項(xiàng)正在進(jìn)行的工作,但基本上目前如果您為乳制品輸入“是”,那么它應(yīng)該將它們?nèi)考悠饋?lái)并等于 100。嘗試了很多不同的選擇,卻一無(wú)所獲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貢獻(xiàn)1887條經(jīng)驗(yàn) 獲得超5個(gè)贊

這里的問(wèn)題是,對(duì)于每個(gè)受訪者,您都將他們的答案記錄在問(wèn)題編號(hào)decisions[j]中;j但稍后您可以通過(guò)迭代受訪者編號(hào)來(lái)計(jì)算“是”響應(yīng)的decisions[k]數(shù)量k。

要么decisions[i]表示某位受訪者對(duì)問(wèn)題 的回答i,要么表示i第 3 位受訪者對(duì)問(wèn)題 1 的回答。它不能同時(shí)表示兩者。您需要重新考慮如何存儲(chǔ)這些數(shù)據(jù)。

此外,由于decisions[j]是為每個(gè)受訪者編寫的j,因此每次都會(huì)覆蓋該數(shù)組,這意味著您最終只會(huì)存儲(chǔ)最后一個(gè)受訪者的結(jié)果。

二維數(shù)組可能是一個(gè)很好的解決方案,其中decisions[i][j]表示i第 3 個(gè)受訪者對(duì)問(wèn)題 的回答j。


查看完整回答
反對(duì) 回復(fù) 2024-01-28
?
阿波羅的戰(zhàn)車

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超6個(gè)贊

首先,讓我們格式化代碼。為了存儲(chǔ) 4 個(gè)不同用戶對(duì) 3 個(gè)不同問(wèn)題的決定,您需要您的數(shù)組(數(shù)據(jù)結(jié)構(gòu))是這樣的。另外,看起來(lái)您(目前)只對(duì)有關(guān)乳制品問(wèn)題的決定感興趣。因此,只需在計(jì)算中檢查一下即可。我已經(jīng)更新了代碼并添加了注釋。需要更新存儲(chǔ)結(jié)果的部分以及計(jì)算乳制品總數(shù)的方式。


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

    }

}


查看完整回答
反對(duì) 回復(fù) 2024-01-28
?
Helenr

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


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



        }

}


查看完整回答
反對(duì) 回復(fù) 2024-01-28
  • 3 回答
  • 0 關(guān)注
  • 193 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)