package hello; import java.io.IOException; public class hello { int count = 0; public void check() throws IOException { byte[] str = new byte[20]; System.in.read(str); for (int i = 0; i < str.length - 1; i++) { if (str[i] == 'Z') { count++; } } if(count>0) { throw new IOException("按下了Z键"); } } public static void main(String args[]) { hello ts =new hello(); System.out.println("请输入你想输入的字符串:"); try { ts.check(); return; } catch (IOException ie) { ie.printStackTrace(); }finally { System.out.println("z is "); System.out.print(ts.count); } } }