public class Weekdays {
public static void main(String args[]) {
try{
for(int i=1 ;i<9; i++)
System.out.println(i+"----"+giveName(i));
}catch(InvalidIndexException e){System.out.println(e.toString());
}
finally{System.out.println("These days makes up a week.");
}
}
public static String giveName(int d) throws InvalidIndexException{
String name;
switch(d){
case 1:name="Monday "; break;
case 2:name="Tuesday "; break;
case 3:name="Wednesday "; break;
case 4:name="Thursday "; break;
case 5:name="Friday "; break;
case 6:name="Saturday "; break;
case 7:name="Sunday "; break;
default:throw new InvalidIndexException();
}
return name;
}
}
class InvalidIndexException extends Exception {
private static final long serialVersionUID = 1L;
private int i;
void InvalidIndexExceptio(int a){
i=a;
}
public String toString(){
return i+" is put of boundary-- 0<i<8 ";
}
}
打印结果
1----Monday
2----Tuesday
3----Wednesday
4----Thursday
5----Friday
6----Saturday
7----Sunday
[color=red]0[/color] is put of boundary-- 0<i<8 //[color=red]我认为i应该是8啊,为什么是0啊?[/color]These days makes up a week.