in.nextInt();只能是输入整数,如果不是整数会抛出异常的
你可以使用String str = in.nextLine();
然后判断
if(str.equals("z")){
throw new Exception("键盘按到z");
}
public class MyMain02 {
static void abc() throws Exception {
System.out.println("开始调用啦");
java.util.Scanner sc=new java.util.Scanner(System.in);
if(sc.nextByte()=='z'){
throw new Exception("键盘按到z");
}
}
public static void main(String[] args) {
try {
abc();
} catch(Exception e) {
System.out.println("我是catch语句" + e);
}
}
}
//
// 这个写法
//
您看看能否符合您的要求
public class Test {
public static void main(String[] args) {
try {
inputString();
} catch (Exception e) {
System.out.println("我是catch语句" + e);
}
}
public static void inputString() throws Exception {
while (true) {
char input = (char) System.in.read();
if (input == 'z') {
throw new Exception("键盘按到了z");
}
}
}
}
String s;
System.out.println("请输入字符:");
Scanner in = new Scanner(System.in) ;
s = in.next();
if("z".equals(s)){
System.out.println("你输入z了。");
}else{
System.out.println("你输入的字符不是z,内容是:"+s);
}
abc 传入 参数 String str ,
if('z'.equals(str)){
throw new excetion('');
}