import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) throws IOException {
//write your code here
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
while (t!=0){
int a;
a = sc.nextInt();
while (a>10)
a/=10;
System.out.println(+a);
}
}
}
最佳答案
如果你只想处理异常,你必须用 try/catch block 包围它。如果您想查找数字的第一位,请使用以下代码:
import java.util.*;
public class Main {
public static void main(String args[]){
try {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number: ");
int number = sc.nextInt();
boolean state = true;
while (state) {
if ((number >= 10) || (number <= -10)) {
number /= 10;
} else {
if (number < 0) {
number *= -1;
System.out.println(number);
state = false;
} else {
System.out.println(number);
state = false;
}
}
}
} catch (InputMismatchException e){
System.out.println("Your input doest match");
}
}
}
关于java - 如何处理以下代码中的运行时错误?我正在编写一个代码来查找java中数字的第一位数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59794708/