I'm creating a method in my program that tells whether the if the user is at risk of covid through these question markers that has specific health risk categories (low, medium, high). The expected output should be for example if the users enters "y"(yes) in "a. fever" the answer would fall under the equivalent marker that is low (risk), if enters "n" then just continue to the next and so on then after answering everything the accumulative number of the markers (low, medium high) will determine if that person is at risk or not. (hope this clarifies the overview of the code)
I'm trying to loop through 2 different 1d arrays with the same length the result should take only 1 element through each of the arrays but I'm having a real difficulty on how to do this. Below is my code
System.out.println(
"1. Are you experiencing or did you have any of the following in the last 14 following in the last 14 days?");
int low = 0, medium = 0, high = 0;
String[] no1SubQuestion = new String[10];
no1SubQuestion[0] = "";
no1SubQuestion[1] = "a. Fever";
no1SubQuestion[2] = "b. Cough and/or Colds";
no1SubQuestion[3] = "c. Body Pains";
no1SubQuestion[4] = "d. Sore Throat";
no1SubQuestion[5] = "e. Fatigue or Tiredness";
no1SubQuestion[6] = "f. Headache";
no1SubQuestion[7] = "g. Diarrhea";
no1SubQuestion[8] = "h. Loss of Taste or Smell";
no1SubQuestion[9] = "i. Difficulty of Breathing";
int[] no1SubQuestHealthRisk = new int[10];
no1SubQuestHealthRisk[0] = 0;
no1SubQuestHealthRisk[1] = low;
no1SubQuestHealthRisk[2] = medium;
no1SubQuestHealthRisk[3] = high;
no1SubQuestHealthRisk[4] = high;
no1SubQuestHealthRisk[5] = high;
no1SubQuestHealthRisk[6] = high;
no1SubQuestHealthRisk[7] = high;
no1SubQuestHealthRisk[8] = high;
no1SubQuestHealthRisk[9] = high;
for (int i = 0; i < no1SubQuestion.length; i++) {
for (int j = 0; j < no1SubQuestHealthRisk.length; j++) {
try {
System.out.println(no1SubQuestion[i]);
input.nextLine();
System.out.printf("(Enter \"Y\" if YES, \"N\" if NO only)\n>> ");
String answer = input.nextLine();
if (!answer.equalsIgnoreCase("y") & !answer.equalsIgnoreCase("n")) {
throw new InputMismatchException("invalid input");
}
boolean isYes = answer.equalsIgnoreCase("y");
if (!isYes) {
continue;
}
no1SubQuestHealthRisk[j] += 1;
} catch (InputMismatchException e) {
System.out.println("Error!!! (Please enter \"Y\" if YES, \"N\" if NO only)");
input.nextLine();
}
}
}
if (high > 0) {
result = "HIGH";
} else if (medium > 0 && high == 0) {
result = "MEDIUM";
} else if ((low == 1 || low == 2) && (medium == 0 && high == 0)) {
result = "LOW";
} else {
result = "NO";
}
return result;
The result should print one element on the no1SubQuestion array then after the user inputs, the answer should be stored to the equivalent int (low, medium, high) inside the no1SubQuestHealthRisk array
i
as an index into both arrays, if they are guaranteed to be the same length.low, medium, high
does? all 3 of them assign0
and you are not modifying them.