Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to extract answers from for loop

I can’t figure out how to correctly write my for loop statement that will give me the correct score. I bolded the code that is what I can’t figure out how to write correctly. Anytime I run my program I end up with the first result of (rslt < 3) no matter what numbers I enter.

package module1.assignment;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String options[] = {
            "mild or spicy",
            "tea or coffee",
            "breakfast or " +
            "brunch",
            "summer or winter",
            "paper or plastic"
        };
        int answers[] = new int[options.length];
        String result[] = new String[answers.length];
        boolean bool = true;

        while (true) {
            for (int i = 0; i < options.length; i++) {
                System.out.println("Enter 0 for the preference on the left\n" +
                    "Enter 1 for the preference on the right");
                System.out.println("Do you prefer " + options[i] + "?");
                answers[i] = scanner.nextInt();
                System.out.println("Do you prefer " + options[i + 1] + "?");
                answers[i] = scanner.nextInt();
                System.out.println("Do you prefer " + options[i + 2] + "?");
                answers[i] = scanner.nextInt();
                System.out.println("Do you prefer " + options[i + 3] + "?");
                answers[i] = scanner.nextInt();
                System.out.println("Do you prefer " + options[i + 4] + "?");
                answers[i] = scanner.nextInt();
                break;
            }
            for (int i = 0; i < answers.length; i++) {
                result[i] = [answers[i]];
            }

            int rslt = getScore(result);
            if (rslt < 3)
                System.out.println("You prefer life to be calm and organized");
            else if (rslt > 3)
                System.out.println("You prefer life to be spontaneous and active.");
            else
                System.out.println("You prefer a good balance in life");

            System.out.println("Enter 0 to exit program or 1 to run again");
            int out = scanner.nextInt();

            if (out == 0)
                bool = false;
            if (!bool)
                System.exit(0);
        }
    }
    static int getScore(String[] result) {
        int score = 0;

        for (int i = 0; i < result.length; i++) {
            switch (result[i]) {
                case "spicy":
                    score++;
                    break;
                case "coffee":
                    score++;
                    break;
                case "breakfast":
                    score++;
                    break;
                case "winter":
                    score++;
                    break;
                case "paper":
                    score++;
                    break;
            }
        }
        return score;
    }
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have modified your code according to my understanding of the code.
It works just exactly like you may have wanted.

package module1.assignment;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[][] options = {
                {"mild", "spicy"},
                {"tea", "coffee"},
                {"brunch", "breakfast"},
                {"summer", "winter"},
                {"plastic", "paper"}
        };
        int[] answers = new int[options.length];

        do {
            System.out.println("Enter 0 for the preference on the left\n" +
                    "Enter 1 for the preference on the right");
            for (int i = 0; i < options.length; i++) {
                System.out.println("Do you prefer " + options[i][0] +
                        " or " + options[i][1] + "?");
                answers[i] = scanner.nextInt();
            }

            int result = getScore(answers);
            if (result < 3)
                System.out.println("You prefer life to be calm and organized");
            else if (result > 3)
                System.out.println("You prefer life to be spontaneous and active.");
            else
                System.out.println("You prefer a good balance in life");

            System.out.println("Enter 0 to exit program or 1 to run again");
        } while (scanner.nextInt() != 0);
    }
    static int getScore(int[] answers) {
        int score = 0;
        for (int answer : answers) if (answer == 1) score++;
        return score;
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading