我写2 ArrayList
类型String
包含日期和可能的时间,我希望用户输入input,然后检查输入是否不是来自数组,它将显示一条消息,提示输入无效,用户再次输入。但是我的代码的结果给了我相反的:(当我在数组外输入内容时,它将接受它
我的代码怎么了?
请给我看看正确的代码:(
package javaapplication19;
import java.util.ArrayList;
import java.util.Scanner;
public class JavaApplication19 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
ArrayList<String> dayArray = new ArrayList<>();
ArrayList<String> timeArray = new ArrayList<>();
dayArray.add("sunday");
dayArray.add("monday");
dayArray.add("tuesday");
dayArray.add("wednesday");
dayArray.add("thursday");
timeArray.add("8am");
timeArray.add("9am");
timeArray.add("10am");
timeArray.add("11am");
timeArray.add("12pm");
timeArray.add("1pm");
timeArray.add("2pm");
timeArray.add("3pm");
timeArray.add("4pm");
System.out.println("please enter day :");
String a1 = input.nextLine();
for (int g = 0; g < dayArray.size(); g++){
if (dayArray.get(g).equals(a1))
System.out.println("invalid day , please enter another day : ");
a1 = input.nextLine();
}
System.out.println("please enter time : ");
String a2 = input.nextLine();
for (int s = 0; s < timeArray.size(); s++) {
if (timeArray.get(s).equals(a2))
System.out.println("invalid time , please enter another time : ");
a2 = input.nextLine();
}
}
}
答案 0 :(得分:2)
您可以这样:
XAxis
不需要遍历数组。方法contains()为您完成了
答案 1 :(得分:1)
您可以使用以下代码查找if条件的变化。
System.out.println("please enter time : ");
String a2 = input.nextLine();
for (int s = 0; s < timeArray.size(); s++) {
if (!timeArray.get(s).equals(a2))
System.out.println("invalid time , please enter another time : ");
a2 = input.nextLine();
}
答案 2 :(得分:1)
您的代码应该是这样的。
System.out.println("please enter day :");
boolean validDate = false;
while(!validDate){
String a1 = input.nextLine();
if(dayArray.contains(a1)){
System.out.println("invalid day , please enter another day : ");
}else{
validDate=true;
}
}
boolean validHour = false;
System.out.println("please enter a time : ");
while(!validHour){
String a2 = input.nextLine();
if(timeArray.contains(a2)){
System.out.println("invalid time , please enter another time : ");
}else{
validHour=true;
}
}
答案 3 :(得分:0)
我确实重构了您的代码,并添加了一些注释,以使其更容易理解
Scanner input = new Scanner(System.in);
List<String> dayArray = Arrays.asList("sunday","monday","tuesday","wednesday","thursday");
List<String> timeArray = Arrays.asList("8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm");
String a1 = "";
String a2 = ""; //Store user input
boolean nextInput = false; // flag indicate we continue or we force user re-enter information
//try to read the day input
while (!nextInput) {
System.out.println("please enter day :");
a1 = input.nextLine();
nextInput = dayArray.contains(a1);
}
//try to read the time input
nextInput = false; //<= reset flag
while(!nextInput) {
System.out.println("please enter time : ");
a2 = input.nextLine();
nextInput = timeArray.contains(a2);
}
System.out.println("day :" + a1);
System.out.println("time :" + a2);
System.out.println("program finished");
input.close(); //close scanner
答案 4 :(得分:0)
这样写
WARNING:socketIO-client:insta-menorah.herokuapp.com:39203//socket.io [engine.io waiting for connection] HTTPConnectionPool(host='insta-menorah.herokuapp.com', port=39203): Max retries exceeded with url: //socket.io/?EIO=3&transport=polling&t=1543479369533-0 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x1027c3a90>: Failed to establish a new connection: [Errno 60] Operation timed out',))