Java 如何在一个循环中遍历 2 个不同的数组
How to loop through 2 different arrays in one loop in Java
我正在我的程序中创建一种方法,通过这些具有特定健康风险类别(低、中、高)的问号来判断用户是否有感染新冠肺炎的风险。预期的输出应该是,例如,如果用户在“a.fever”中输入“y”(是),答案将落在低(风险)的等效标记下,如果输入“n”,则继续下一个和依此类推,然后在回答完所有问题后,标记的累积数量(低、中高)将确定该人是否处于危险之中。 (希望这能澄清代码的概述)
我正在尝试循环遍历 2 个具有相同长度的不同一维数组,结果在每个数组中应该只需要 1 个元素,但我在如何执行此操作方面遇到了真正的困难。下面是我的代码
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;
结果应在 no1SubQuestion 数组上打印一个元素,然后在用户输入后,答案应存储到 no1SubQuestHealthRisk 数组中的等效整数(低、中、高)
现在使用给定的代码输出是这样的
但我正在尝试这样做
您的问题索引以 1
开头,因此应以 i=1
开头。
你不需要 2 个数组,一个就可以了。
而不是第二个 for 循环,while 循环会做得更好,因为它只是为了获得有效输入。
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;
我正在我的程序中创建一种方法,通过这些具有特定健康风险类别(低、中、高)的问号来判断用户是否有感染新冠肺炎的风险。预期的输出应该是,例如,如果用户在“a.fever”中输入“y”(是),答案将落在低(风险)的等效标记下,如果输入“n”,则继续下一个和依此类推,然后在回答完所有问题后,标记的累积数量(低、中高)将确定该人是否处于危险之中。 (希望这能澄清代码的概述)
我正在尝试循环遍历 2 个具有相同长度的不同一维数组,结果在每个数组中应该只需要 1 个元素,但我在如何执行此操作方面遇到了真正的困难。下面是我的代码
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;
结果应在 no1SubQuestion 数组上打印一个元素,然后在用户输入后,答案应存储到 no1SubQuestHealthRisk 数组中的等效整数(低、中、高)
现在使用给定的代码输出是这样的
但我正在尝试这样做
您的问题索引以 1
开头,因此应以 i=1
开头。
你不需要 2 个数组,一个就可以了。
而不是第二个 for 循环,while 循环会做得更好,因为它只是为了获得有效输入。
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;