公共课AppointmentSchedule {
private static final int NUM_APPOINTMENTS = 6;
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] scheduled = new String[NUM_APPOINTMENTS];
Scanner consoleScanner = new Scanner(System.in);
int i;
String name;
for (int z = 0; z < NUM_APPOINTMENTS; z++) {
scheduled[z] = "";
}
System.out.println("To schedule an appointment, Please enter a time between 1PM to 6PM");
do {
i = consoleScanner.nextInt();
try {
if (i >= 1 && i <= 6) {
try {
if (scheduled[i] == "") {
System.out.println("Please enter your name.");
name = consoleScanner.next();
scheduled[i] = name;
System.out.println("Thank you " + name
+ ", you have been scheduled for " + i
+ " PM.\n");
System.out
.println("To schedule an appointment, Please enter a time between 1PM to 6PM");
} else {
throw new TimeInUseException();
}
} catch (TimeInUseException ex1) {
System.out.println(ex1.getMessage());
}
} else
throw new InvalidTimeException();
} catch (InvalidTimeException ex) {
System.out.println(ex.getMessage());
}
} while ();
consoleScanner.close();
}
}
在计划[i]填充6个元素之后,有什么技术可以结束do while循环?
看起来像是:while(预定[z]!= 6)?
答案 0 :(得分:2)
执行此操作while(i<=5);
即使i=6
此处i
是您继续增加的变量,这也会让您的循环继续运行
答案 1 :(得分:2)
只需跟踪用户输入的输入数量即可。这可以通过声明
来完成int count = 0;
在do...while
循环之前并将其从内部if
的主体递增:
if (scheduled[i - 1] == "") {
System.out.println("Please enter your name.");
name = consoleScanner.next();
scheduled[i - 1] = name;
System.out.println("Thank you " + name
+ ", you have been scheduled for " + i
+ " PM.\n");
count++; /* Note this */
System.out.println("To schedule an appointment, Please enter a time between 1PM to 6PM");
}
最后,将条件更改为
} while (count < 6);