2014-11-05 74 views
-1

所以我有一个类的任务,我完全卡住了。我整个学期都在这里潜伏,并且找到了我需要的东西,但是这次我迷路了。我需要帮助一个循环内的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"); 
} 


} 
+0

请发表意见。尽管如此,只是通过观察这些渔获物看起来是错误的。 – Azar 2014-11-05 20:17:28

+0

将timeSlot [i]添加到固定的后半部分,现在我已经有另外一个 – 2014-11-05 20:23:37

+0

您的代码不能编译,并且格式不正确,请您将代码提交给适当的选项卡? – 2014-11-05 20:25:23

回答

0

更改您的语句来if(timeSlot[i] < 1 || timeSlot[i] > 6)

+0

谢谢,以便修复那个错误,但它仍然有更多。 – 2014-11-05 20:20:57

1

您的catch块位于错误的位置。他们应该立即你的尝试块而不是里面。我并不完全明白你在循环中对你的想要做什么,因此可能还有其他一些问题,但这应该是一个开始。

+0

好的,我明白,他们必须分开,改变这些,并解决了这个问题。谢谢 – 2014-11-05 20:26:03

0

你在第40行得到的是一个合成错误。您必须在逻辑OR语句中明确指定要输入的条件 - Java编译器在这里不会猜测您想要检查timeSlot数组中的值是否大于6.您拥有什么要做的是类型:

if(timeSlot[i] < 1 || timeSlot[i] > 6){ 
    throw new InvalidTimeException(); 
}   

除此之外,你的catch块超出范围,不能在for循环。

+0

谢谢我已经改变了,现在我只需要它接受数字,如果它不为空,第46行。 – 2014-11-05 20:28:34

+0

只需添加一个右括号,如 'else if(timeSlot [i]!= null ){ throw(new TimeInUseException()); }' – lesz3k 2014-11-05 20:29:47

+0

我在空白后添加了括号。它仍然给我一个错误,说明它不可编译 – 2014-11-05 20:33:16