所以我有一个类的任务,我完全卡住了。我整个学期都在这里潜伏,并且找到了我需要的东西,但是这次我迷路了。我需要帮助一个循环内的try catch语句。该任务要求我们让人们输入他们的姓名,并选择一个时间表1,2,3,4,5,6,如果超出该范围或时间已被占用,则有例外。继承人到目前为止我所拥有的。当运行时,它会让我把第一个名字放入并且一次,但是给第40行,这是"if(timeSlot[i] <1 || >6){ "
行的错误。如果语句内的try catch语句没有检测到try/catch部分
任何帮助,将不胜感激,谢谢
import java.util.Scanner;
public class Scheduler {
Scanner input = new Scanner(System.in);
public static void main(String[] args) throws TimeInUseException,
InvalidTimeException {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
String name[] = new String[6];
int timeSlot[] = new int[6];
//as long as there is less than 6 it will keep running
int d = 0;
while (d < 6) {
//try block to see if the time slot has been taken
try {
for (int i = 0; i < name.length; i++) {
//ask for the name
System.out.println("Please type your name: ");
name[i] = input.nextLine();
//ask for the time
System.out.println("Please select a time: 1, 2, "
+ "3, 4, 5, 6");
timeSlot[i] = input.nextInt();
// if the time is less than 1 or greater than 6
// it will throw an invalid time exception
if (timeSlot[i] < 1 || timeSlot[i] > 6) {
throw (new InvalidTimeException());
}
// if the time slot is not equal to null(not empty)
// it will throw a time in use exception
if (timeSlot[i] != null) {
throw (new TimeInUseException());
}
}
} // catch for invalid time
catch (InvalidTimeException ite) {
ite.getMessage();
} //catch for time in use
catch (TimeInUseException tiue) {
tiue.getMessage();
}
d++;
}
for (int i = 0; i < 6; i++) {
System.out.println(name[i] + " has an appointment at: "
+ timeSlot[i]);
}
}
}
例外每个请求
public class InvalidTimeException extends Exception{
//sets the String to say time is in use
public InvalidTimeException(String message) {
super(message);
message = "That time is invalid, please enter either 1, 2, 3, 4, 5, or 6";
}
InvalidTimeException() {
throw new UnsupportedOperationException("That time is invalid, please enter either 1, 2, 3, 4, 5, or 6"); //To change body of generated methods, choose Tools | Templates.
}
}
,第二个
public class TimeInUseException extends Exception {
//sets the String to say time is in use
public TimeInUseException(String message) {
super(message);
// message = "That time is already in use, please select another";
}
TimeInUseException() {
throw new UnsupportedOperationException("That time is already in use, "
+ "please select another");
}
}
请发表意见。尽管如此,只是通过观察这些渔获物看起来是错误的。 – Azar 2014-11-05 20:17:28
将timeSlot [i]添加到固定的后半部分,现在我已经有另外一个 – 2014-11-05 20:23:37
您的代码不能编译,并且格式不正确,请您将代码提交给适当的选项卡? – 2014-11-05 20:25:23