我试图从数组中获取一个元素,稍后在我的代码中使用它,但我的代码中有一些逻辑错误。我希望用户进入他/她需要清洁的那一天,然后打印出清洁日。这是代码:
import java.util.Scanner;
public class arrayLoopTest {
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
int i = 0;
while (true) {
System.out.println("On what day should the cleaning be scheduled ? ");
String schedule = scr.nextLine();
for (i = 0; i < days.length; i++) {
if (days[i].equals(schedule)) {
System.out.println("Cleaning scheduled for " + schedule);
} else {
System.out.println("Invalid, You have to choose one of these days: \n " + days[i]);
}
}
}
}
}
控制台输出
On what day should the cleaning be scheduled ?
Thursday
Invalid, You have to choose one of these days:
Monday
Invalid, You have to choose one of these days:
Tuesday
Invalid, You have to choose one of these days:
Wednesday
Cleaning scheduled for Thursday
Invalid, You have to choose one of these days:
Friday
Invalid, You have to choose one of these days:
Saturday
Invalid, You have to choose one of these days:
Sunday
On what day should the cleaning be scheduled ?
答案 0 :(得分:1)
一种非常简单的方法是添加另一个变量:
String savedVar = "";
....
if (days[i].equals(schedule)) {
savedVar = schedule; //save your user input here
System.out.println("Cleaning scheduled for " + schedule);
} else {
System.out.println("Invalid, You have to choose one of these days: \n " + days[i]);
}
然后在你的代码中使用savedVar。
答案 1 :(得分:1)
当检查是否有任何天匹配时,您正在检查它是否与day[i]
匹配并立即打印出结果(在您甚至检查了阵列的其余部分以进行匹配之前)。
一种可能的解决方案是跟踪用户输入是否与布尔匹配了一天。尝试这样的事情:
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int i = 0;
boolean validEntry;
while (true) {
validEntry = false;
System.out.println("On what day should the cleaning be scheduled ? ");
String schedule = scr.nextLine();
for (i = 0; i < days.length; i++) {
if (days[i].equals(schedule)) {
System.out.println("Cleaning scheduled for " + schedule);
validEntry = true;
}
}
if (!validEntry) {
System.out.println("Invalid, You have to choose one of these days: \n" + Arrays.toString(days));
}
}
}
请注意,我也改变了
System.out.println("Invalid, You have to choose one of these days: \n" + days[i]);
到这个
System.out.println("Invalid, You have to choose one of these days: \n " + Arrays.toString(days));
答案 2 :(得分:1)
请注意,有一种更有效,更简单的方法可以从循环中获取数组元素,但这对初学者来说应该很容易理解。
你有一个for循环很棒,但是你要在用户之前打印文本检查整个数组以查看用户输入是否与数组中的任何值匹配。我所做的是做了一个名为dayIsValid的布尔值,它检查用户输入的字符串是否为有效日。如果该日有效,则打印出所需的消息。请注意,只有在查看整个数组后才能打印此消息。
我注意到的另一件事是,你试图打印String数组中的所有日子,并且只打印一天。为了解决这个问题,我在String数组中循环,打印出值。希望这个解释对你有帮助。如果您有任何疑问,请随时在下面发表评论
Scanner scr = new Scanner(System.in);
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
boolean continueLooping = true;
//be aware that this while loop will execute infinitely, unless you change continuelooping to false inside the while loop
while (continueLooping) {
System.out.println("On what day should the cleaning be scheduled ? ");
String dayScheduledForCleaning = scr.next();
boolean dayIsValid = false;
for (int i = 0; i < days.length; i++) {
if (days[i].equalsIgnoreCase(dayScheduledForCleaning)) {
System.out.println("Cleaning scheduled for " + dayScheduledForCleaning);
dayIsValid = true;
break;
}
}
if(! dayIsValid) {
System.out.println("Invalid, You have to choose one of these days:");
for(int i = 0; i < days.length; i++)
System.out.println(days[i]);
}
}
答案 3 :(得分:1)
我认为逻辑错误是您在循环播放数组时打印响应,因此它总是打印出“无效...”至少六次。
相反,您可以循环遍历数组,直到找到匹配项,然后根据是否找到匹配项打印响应:
boolean scheduleIsValid = false;
for (i = 0; i < days.length; i++) {
if (days[i].equals(schedule)) {
scheduleIsValid = true;
break; // Leave the for loop, we've already found what we were looking for.
}
}
if (scheduleIsValid) {
System.out.println("Cleaning scheduled for " + schedule);
break; // Leave the while loop.
} else {
System.out.println("Invalid, You have to choose one of these days: \n " + days);
}
答案 4 :(得分:1)
试试这个:
import java.util.Arrays;
import java.util.Scanner;
public class ArrayLoopTest {
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
String[] days = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
int i = 0;
while (true) {
System.out.println("On what day should the cleaning be scheduled ? ");
String schedule = scr.nextLine();
for (i = 0; i < days.length; i++) {
if (days[i].equals(schedule)) {
System.out.println("Cleaning scheduled for " + schedule);
break;
}
}
if (i == days.length){
System.out.println("Invalid, You have to choose one of these days: \n " + Arrays.asList(days));
}
}
}
}
如果您担心性能,可以使用HashSet。如果您不知道如何使用只是发送消息,我发布解决方案。
答案 5 :(得分:1)
Java Arrays
实用程序是您的最佳选择。您可以从阵列中取出ArrayList
而不是手动循环,而是使用IndexOf
查找您的日期。
List listOfDays = Arrays.asList(days);
int dayIndex = listOfDays.IndexOf(schedule);
if (dayIndex >= 0)
{
System.out.println("Cleaning scheduled for " + listOfDays[dayIndex]);
}
else
{
System.out.println("Invalid " + schedule); // Perform error handling as you wish
}
来源:
http://www.tutorialspoint.com/java/util/arrays_aslist.htm
http://www.tutorialspoint.com/java/util/arraylist_indexof.htm