具有多种方法的字符串数组,同时保持计

时间:2015-10-06 03:15:35

标签: java

在askQuizQuestion方法中如何运行字符串数组的每个元素?

这是一个简单的测验,但我在输出时将丢失的数组中的每个元素初始化为我的askQuizQuestion方法。

每个使用响应都是是或否。如果是,那么+1到运行计数,在最后一个问题之后,总计数返回到printSurveyResults方法以确定要打印的响应。

public class Quiz {

    public static void printSurveyResults(int answerCount)
    {
        if (answerCount >= 0 && answerCount <= 2)
            {System.out.println ("You are more exhausted than stressed out.");}

        else if (answerCount >= 3 && answerCount <= 5)
            {System.out.println ("You are beginning to stress out.");}      

        else if (answerCount >= 6 && answerCount <= 8)
            {System.out.println ("You are possibly stressed out.");}

        else if (answerCount >= 9 && answerCount <= 12)
            {System.out.println ("You are probably stressed out.");}
    }           

    public static int askQuizQuestion(String prompt, Scanner keyboard)
    {
        int count = 0;
        int i;
        for (i = 0; i < 12; i++);
            System.out.println(prompt);
        if (keyboard.equals("yes"))
            {count++;}

        printSurveyResults(count);
    }   

    public static void main(String[] args) 
    {
        String[] question = new String[12];
        question [0] = "I find myself less eager to go back to work or to resume my chores after a weekend.";
        question [1] = "I feel less and less patient and/or sympathetic listening to other people’s problems.";
        question [2] = "I ask more “closed-ended questions to discourage dialogue with friends and co-workers than “open-ended” ones to encourage it."; 
        question [3] = "I try to get away from people as soon as I can.";
        question [4] = "My dedication to work, exercise, diet, and friendships is waning.";
        question [5] = "I am falling further behind in many of the responsibilities in my life.";
        question [6] = "I am losing my sense of humor.";
        question [7] = "I find it more and more difficult to see people socially.";
        question [8] = "I feel tired most of the time.";
        question [9] = "I don’t seem to have much fun anymore.";
        question [10] = "I feel trapped. ";
        question [11] = "I know what will make me feel better, but I just can’t push myself to do it and I’ll “Yes, but” any suggestions that people make.";
    }
}

1 个答案:

答案 0 :(得分:0)

通过轻微的重构查找下面的代码。您的挑战似乎是阅读Java中的用户输入。这个SO线程:How can I get the user input in Java显示了不同的技术。我在下面的代码中使用了Scanner类中的方法next()

import java.util.Scanner;

public class Quiz {

    private static final String[] questions = {"I find myself less eager to go back to work or to resume my chores after a weekend.",
            "I feel less and less patient and/or sympathetic listening to other people’s problems.",
            "I ask more “closed-ended questions to discourage dialogue with friends and co-workers than “open-ended” ones to encourage it.",
            "I try to get away from people as soon as I can.",
            "My dedication to work, exercise, diet, and friendships is waning.",
            "I am falling further behind in many of the responsibilities in my life.",
            "I am losing my sense of humor.",
            "I find it more and more difficult to see people socially.",
            "I feel tired most of the time.",
            "I don’t seem to have much fun anymore.",
            "I feel trapped. ",
            "I know what will make me feel better, but I just can’t push myself to do it and I’ll “Yes, but” any suggestions that people make."};

    public static void printSurveyResults(int answerCount) {

        if (answerCount >= 0 && answerCount <= 2) {
            System.out.println("You are more exhausted than stressed out.");
        } else if (answerCount >= 3 && answerCount <= 5) {
            System.out.println("You are beginning to stress out.");
        } else if (answerCount >= 6 && answerCount <= 8) {
            System.out.println("You are possibly stressed out.");
        } else if (answerCount >= 9 && answerCount <= 12) {
            System.out.println("You are probably stressed out.");
        }
    }


    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);  // Reading from System.in
        String response;
        int count = 0;
        for (int i = 0; i < questions.length; i++) {
            System.out.println(questions[i]);
            response = reader.next();
            if (response.equals("yes")) {
                count++;
            }
        }
        printSurveyResults(count);

    }

}