【问题标题】:How to reuse user inputs from a loop如何重用循环中的用户输入
【发布时间】:2014-11-19 19:34:46
【问题描述】:

我是 Java 新手,被要求编写一个问卷调查程序。它涉及填写是/否答案并以百分比的形式总结这些答案。

到目前为止,我已经完成了输入,并希望将用户的这些是/否输入用作 1/0,将这些输入相加,从结果中除以 totalQuestions 并乘以 100 以获得是/的百分比每个问题都没有。但可惜我碰壁了!!

我使用了for 循环来迭代问题和参与者,并且可以看到(在调试时)输入的答案被分配给我的变量 - answer[1][0]answer[1][1]、.... 这告诉我需要的值在那里,但如何将其带到下一步?

我在“//问卷调查结果”下方添加了一些内容以显示所需的结果,但这并不能让程序轻松适应更多参与者/问题。

我已将我的程序(到目前为止)粘贴在下面,非常感谢有关如何获得所需结果的任何建议/建议。可能有比我在下面所做的更快/更简单的方法,所以如果我的编码很差,请道歉 - 初学者状态! 感谢您的帮助。

import java.util.Scanner;

public class Questionnaire {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter Yes or No:");

        // Participants
        final int PARTICIPANTS = 3;

        // Questions
        int numQuestions = 3;
        String[] question = new String[numQuestions];
        question[0] = "Have you ever shopped online? ";
        question[1] = "Do you use online banking? ";
        question[2] = "Do you have an email account? ";

        // Participant Loop
        int[][] answer = new int[PARTICIPANTS+1][numQuestions];
        for(int i = 1; i <= PARTICIPANTS; i++)
        {
            System.out.println("Participant " + i);

            // Question Loop
            for(int j = 0; j < numQuestions; j++)
            {
                // Answer yes or no only
                boolean valid = false;
                do
                {
                    System.out.print(question[j]);
                    String userInput = in.next();
                    if(!userInput.equalsIgnoreCase("yes") &&
                            !userInput.equalsIgnoreCase("no"))
                    {
                        System.out.println("Please enter Yes or No");
                    }
                    else
                    {
                        valid = true;
                        if(userInput.equalsIgnoreCase("yes"))
                        {
                            answer[i][j] = 1;
                        }
                        else
                        {
                            answer[i][j] = 0;
                        }
                    }
                } while(!valid);
            }
            System.out.println();
        }

        // Questionnaire results
        int q1Total = answer[1][0] + answer[2][0] +
                answer[3][0];
        int q1YesPercent = (q1Total*100)/PARTICIPANTS;

        int q2Total = answer[1][1] + answer[2][1] +
                answer[3][1];
        int q2YesPercent = (q2Total*100)/PARTICIPANTS;

        int q3Total = answer[1][2] + answer[2][2] + 
                answer[3][2];
        int q3YesPercent = (q3Total*100)/PARTICIPANTS;

        System.out.println("Survey Results\t\t\t Yes\t No");
        System.out.println(question[0] + "\t " + q1YesPercent +
                "%\t " + (100-q1YesPercent) + "%");
        System.out.println(question[1] + "\t " + q2YesPercent +
                "%\t " + (100-q2YesPercent) + "%"); 
        System.out.println(question[2] + "\t " + q3YesPercent +
                "%\t " + (100-q3YesPercent) + "%"); 
    }
}

【问题讨论】:

  • 您的问题到底是什么?您能否更具体地说明您需要哪些帮助?
  • 您可以使用例如int[] totals = new int[numQuestions] 并迭代answers 来填充它。您也可以使用float[] qPercents = new float[numQuestions]。 (注意qPercents 必须是浮点数)

标签: java loops


【解决方案1】:

除以PARTICIPANTS 时可能会出现舍入错误,因为您使用的是int 变量。您可以将 percent 更改为 double 并使用 printf。

    // Questionnaire results
    System.out.println("Survey Results\t\t\t Yes\t No");

    for (int i=0;i<numQuestions;i++) {
            int total = 0;

            for (int j=0;j<PARTICIPANTS;j++)
                    total += answer[j][i];

            int percent = (total * 100) / PARTICIPANTS;

            System.out.println(question[i] + "\t " + percent + "%\t " + (100-percent) + "%");
    }

【讨论】:

  • 感谢 Forager,这就是我所追求的。我确实必须将循环 j=0 更改为 j=1 以获得所需的结果。我会把这个循环反过来说,即 numQuestions 之前的 PARTICIPANTS 但是在阅读你的代码时它更有意义。非常感谢:)))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多