
求一个java编程高手帮忙啊
实现一个输入程序接受从键盘读入的字符串当字符串中所含字符个数少于程序设定的上限时输出这个字符串否则抛出mystringexceptionl异常在异常处理中要求重新输入新的...
实现一个输入程序 接受从键盘读入的字符串 当字符串中所含字符个数少于程序设定的上限时 输出这个字符串 否则抛出mystringexceptionl异常 在异常处理中要求重新输入新的字符串或中断程序运行
展开
2个回答
展开全部
import java.util.Scanner;
public class Du {
public static void main(String[] args) throws MyStringException {
Scanner scanner = new Scanner(System.in);
final int limit = 15;//上限
do{
System.out.print("Please input some string less than " + limit + ":");
String input = scanner.nextLine();
if(input.length() > limit){
throw new MyStringException("Too long!The string should be less than " + limit);
}else{
System.out.println(input);
}
}while(true);
}
}
class MyStringException extends Exception{
public MyStringException(String string) {
super(string);
}
}
-----------------
Please input some string no less than 15:1234567890abcde
1234567890abcde
Please input some string no less than 15:asf
asf
Please input some string no less than 15:01234567890123456
Exception in thread "main" MyStringException: Too long! The string should be less than 15
at Du.main(Du.java:14)
public class Du {
public static void main(String[] args) throws MyStringException {
Scanner scanner = new Scanner(System.in);
final int limit = 15;//上限
do{
System.out.print("Please input some string less than " + limit + ":");
String input = scanner.nextLine();
if(input.length() > limit){
throw new MyStringException("Too long!The string should be less than " + limit);
}else{
System.out.println(input);
}
}while(true);
}
}
class MyStringException extends Exception{
public MyStringException(String string) {
super(string);
}
}
-----------------
Please input some string no less than 15:1234567890abcde
1234567890abcde
Please input some string no less than 15:asf
asf
Please input some string no less than 15:01234567890123456
Exception in thread "main" MyStringException: Too long! The string should be less than 15
at Du.main(Du.java:14)
追问
Exception in thread "main" MyStringException: Too long! The string should be less than 15
at Du.main(Du.java:14)
是什么意思啊
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询