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;
}
}
CodePudding user response:
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;
}
}
CodePudding user response:
To Fix Your Code
In the first for-loop, you are supposed to loop through the options array. But somehow you unfold the loop within the loop body. To prevent the whole thing loop again, you break the loop immediately. To fix the first loop, write it like this instead. Properly loop through each element, no need to unfold it, no need to break it.
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] "?");
answers[i] = scanner.nextInt();
}
In the second loop, you are supposed to retrieve the selected string from answers and write the string to results. Without modifying your data structure, this can be achieved by using split(" or ") on the option, which gives you an array of string which you can use the answer as index to access. Note that this does not prevent array index out of bound exception if user enter anything other than 0 or 1, which you should totally do, but is out of scope of this question.
for (int i = 0; i < answers.length; i ) {
result[i] = options[i].split(" or ")[answers[i]] ;
}
And there you go.
To Solve Your Task
Alternatively, redesigning the data structure and logic to get rid of the unnecessary string manipulation and comparison is more ideal. You don't even need the result and answers array, simply add up the user input will do (if the user follows your instruction)
int rslt = 0;
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] "?");
rslt = scanner.nextInt();
}
CodePudding user response:
Inside the loop, you continue assigning the result to the same index in the answers list, rather than assigning the result to another index for each input. Because you are not iterating anything, you don't even need the loop. Replace the entire while loop with the code below. Please upvote and accept answer if it solves/helps you with your problem.
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[0] "?");
answers[0] = scanner.nextInt();
System.out.println("Do you prefer " options[1] "?");
answers[1] = scanner.nextInt();
System.out.println("Do you prefer " options[2] "?");
answers[2] = scanner.nextInt();
System.out.println("Do you prefer " options[3] "?");
answers[3] = scanner.nextInt();
System.out.println("Do you prefer " options[4] "?");
answers[4] = scanner.nextInt();
Also, please note this:
for (int i = 0; i < answers.length; i ) {
result[i] = [answers[i]];
}
won't work. Instead of result[i] = [answers[i]];, do result[i] = Integer.parseInt(answers.get(i)).
CodePudding user response:
This is part causing the unexpected behaviour: result[i] = [answers[i]];
From what I understood, you want to implement this:
- For each option, store the user choice like 0 or 1, 0 for left, 1 for right
- For each user choice, store the string value of the choice, i.e., for the 1st question if the user inputs 0, mild should be captured
- Calculate scores by comparing string value of user input against a branching construct, switch case here
The problem is in step 2, the current code result[i] = [answers[i]]; does not express this operation properly.
answers[i] stores the user choice 0 or 1, step 1 operation. So to convert it to the corresponding choice in string step 2 operation, something like this should be done
(options[i].split(" or "))[answers[i]]
Explanation:
- Pick up the complete string for each answer
- Divide the string into two parts(array with 2 indexes 0 and 1), left and right, using a delimiter, " or " in this case
- pick up the left or right based on the value stored in answers[i](the user input)
This should let the code behave as expected :)
There are other aspects that can be improved in the code though, as others have already suggested.
