// Import packages and create main function
import java.util.Scanner;
import java.util.ArrayList;
public class schedule{
public static void main(String []args){
// Creates scanner and ArrayLists
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> mechanics = new ArrayList<Integer>();
ArrayList<Integer> bays = new ArrayList<Integer>();
boolean edit = true;
while(edit == true){
edit = editSchedule(scanner);
// Runs AddorRemove or ends program based on user's previous choice
if(edit == true){
if(AddorRemove(scanner) == true){
add(scanner, mechanics, bays);
}
else{
remove(scanner, mechanics, bays);
}
}
else{
System.out.println("Have a nice day!");
}
}
}
// Asks user if they want to edit the schedule
public static boolean editSchedule(Scanner scanner){
String answer = "b";
while(!(answer.equals("y")) || answer.equals("n")){
System.out.print("Would you like to edit the schedule? (y/n): ");
answer = scanner.nextLine();
if(answer.equals("y") || answer.equals("n")){
break;
}
else{
System.out.println("Invalid response.");
}
}
if(answer.equals("y")){
return true;
}
else{
return false;
}
}
// Asks user if they want to add or remove an element
public static boolean AddorRemove(Scanner scanner){
String aor = "b";
while(!(aor.equals("a")) || aor.equals("r")){
System.out.print("Do you want to add or remove a mechanic and bay? (a/r): ");
aor = scanner.nextLine();
if(aor.equals("a") || aor.equals("r")){
break;
}
}
if(aor.equals("a")){
return true;
}
else{
return false;
}
}
// Checks to see that elements are not present already and adds element to the mechanic and bay ArrayLists
public static void add(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
System.out.print("Enter the mechanic's ID number: ");
int mechanic = scanner.nextInt();
System.out.print("Enter the bay number: ");
int bay = scanner.nextInt();
boolean shouldAdd = true;
for(int i=0; i<=mechanics.size()-1; i++){
if(mechanic == mechanics.get(i)){
System.out.println("Mechanic " + mechanic + " is already booked.");
shouldAdd = false;
}
}
for(int j=0; j<=bays.size()-1; j++){
if(bay == bays.get(j)){
System.out.println("Bay " + bay + " is already booked.");
shouldAdd = false;
}
}
if(shouldAdd == true){
mechanics.add(mechanic);
bays.add(bay);
System.out.println("Mechanic " + mechanic + " booked for bay " + bay + ".");
}
else{
System.out.println("Could not book mechanic " + mechanic + " in bay " + bay + ".");
}
}
// Removes an element from the mechanic and bay ArrayLists
public static void remove(Scanner scanner, ArrayList<Integer> mechanics, ArrayList<Integer> bays){
System.out.println("remove");
}
}
Would you like to edit the schedule? (y/n): y
Do you want to add or remove a mechanic and bay? (a/r): a
Enter the mechanic's ID number: 1
Enter the bay number: 3
Mechanic 1 booked for bay 3.
Would you like to edit the schedule? (y/n): Invalid response.
Would you like to edit the schedule? (y/n):
当您使用scanner.nextint()
时,它不会完成这一行。请将此测试作为一个示例:
@Test
public void testScanner() {
Scanner scanner = new Scanner("1\n2\n3\n4\n");
System.out.println("nextLine: [" + scanner.nextLine() + "]");
System.out.println("nextInt: [" + scanner.nextInt() + "]");
System.out.println("nextLine: [" + scanner.nextLine() + "]");
System.out.println("nextLine: [" + scanner.nextLine() + "]");
}
它产生:
nextLine: [1]
nextInt: [2]
nextLine: []
nextLine: [3]
因此,在add()
方法返回后,将有一个行结束,扫描仪将以空行的形式读取该行。这会给出无效响应
消息。
我添加了一个while循环,这样如果用户输入无效值,代码将重新提示用户输入有效值。但是当用户输入无效值时,代码会进入无限循环。任何帮助都将不胜感激!!
下面的代码循环了两次。第一个while循环要求用户输入(以做出选择)。我把选择默认值设置为“n”,使它变得更简单。 因此,它命中if语句并开始第2个while循环。现在它要求用户进行另一个选择。用户只能输入“a”,因为其他任何东西都会出现错误陷阱。用户输入“a”并得到添加一个数字的提示(变量num=0)。用户输入一个数字。 使用更多信息更新的代码 我已经尝试使用不同的变量进行选择。它不起作用。我想
当用户输入字符串(例如“”)时,程序应该给出输出“”,然后提示用户键入有效输入。当我键入字符串时,我会在线程“main”java.util.InputMismatchException中得到错误消息
我在循环中输入name和num时出错,
我的循环一直在进行,即使我输入了y、yes、n或no。 输出: 抱歉,该答案无效,请重试。是否要重新滚动模具1?不 抱歉,该答案无效,请重试。是否要重新滚动模具1?对 抱歉,该答案无效,请重试。是否要重新滚动模具1?对