如何编写一个java程序,判断键盘输入是否为小写字母z,如果是,就抛出异常。

2025-02-24 15:19:56
推荐回答(5个)
回答1:

in.nextInt();只能是输入整数,如果不是整数会抛出异常的
你可以使用String str = in.nextLine();
然后判断
if(str.equals("z")){
throw new Exception("键盘按到z");

}

回答2:


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);
        }
    }
}

//

// 这个写法

//

回答3:

您看看能否符合您的要求

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");
}
}
}
}

回答4:

 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);
    }

回答5:

abc 传入 参数 String str ,
if('z'.equals(str)){
throw new excetion('');
}