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
right now with the given code the output is this
but im trying to do it like this
CodePudding user response:
Based on the limited information provided, it's probably something along these lines:
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 score = 0;
final int low = 1, medium = 2, high = 3;
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 ) {
try {
System.out.println(no1SubQuestion[i]);
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;
}
score = no1SubQuestHealthRisk[i]; // get the severity from the array and add it to the score
} catch (InputMismatchException e) {
System.out.println("Error!!! (Please enter dddddddddddd\"Y\" if YES, \"N\" if NO only)");
input.nextLine();
}
}
System.out.println("Your score is: " score);```
CodePudding user response:
Your question index starts with 1
, so should start with i=1
.
You don't need 2 arrays, just one will do.
Instead of 2nd for-loop, while-loop will do better, since it's only there to get valid input.
int low = 0, medium = 0, high = 0;
for (int i = 1; i < no1SubQuestion.length; i ) {
System.out.println(no1SubQuestion[i]);
System.out.printf("(Enter \"Y\" if YES, \"N\" if NO only)\n>> ");
while(true) {
try {
String answer = input.nextLine();
if (!answer.equalsIgnoreCase("y") & !answer.equalsIgnoreCase("n")) {
throw new InputMismatchException("invalid input");
}
boolean isYes = answer.equalsIgnoreCase("y");
//in the answer is no, no need to increment.
if (!isYes) {
break;
}
//increment the respective risk categories.
if(i==1)
low ;
else if(i==2)
medium ;
else
high ;
break;
} catch (InputMismatchException e) {
System.out.println("Error!!! (Please enter \"Y\" if YES, \"N\" if NO only)");
}
}
}
if (high > 0)
result = "HIGH";
else if (medium > 0)
result = "MEDIUM";
else if (low > 0)
result = "LOW";
else
result = "NO";
return result;