import java.util.Scanner;
class InvalidSideException extends Exception{
InvalidSideException(String s){
super(s);
}
}
class Square{
int sl;
public Square(int sl) throws InvalidSideException{
if (sl <= 0){
throw new InvalidSideException("Side length must be greater than 0");
}else{
this.sl = sl;
}
}
public int getArea(){
return this.sl * this.sl;
}
}
public class Driver{
public static void main(String[] args){
System.out.print("Enter side length of square:");
Scanner s = new Scanner(System.in);
int x = s.nextInt();
try{
Square sq = new Square(x);
System.out.println(sq.getArea());
}catch(InvalidSideException e){
e.printStackTrace();
}
}
}
我想得到
Enter·side·length·of·square:0
Enter◦side◦length◦of◦square:Side◦length◦must◦be◦greater◦than◦0.
但是
结果总是
Enter◦side◦length◦of◦square:
无法打印Side◦length◦must◦be◦greater◦than◦0.
求问怎么diao'yong