应当用while循环,当输入不满足非0数字,要提示重复输入。
String i = sc.next();
while("0".equals(i) || i.matches("\\D+")){ //\\D+ 表示1-n个非数字
System.out.println("请重新输入:")
i = sc.next();
}
// 到这里就是非0的数字了
另外,你用try catch也可以
while(true){
try{
i=sc.nextInt();
if(i==0){
throw new RuntimeException();
}
Facemain ();
break; //非0数字跳出
}catch(Exception e){
System.out.println("请输入0");
i=sc.nextInt();
}
try catch是异常捕获机制,你try catch中间的代码没有任何异常肯定不会走catch里面的方法
你要的效果直接
if(i==0){
Facemain ();
}else{
System.out.println("请输入0");
i=sc.nextInt();
}
如果你写的这段代码有可能会抛出异常,那么就需要try-catch-finally来对这个异常进行处理