Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理以下代码中的运行时错误?我正在编写一个代码,用java查找数字的第一位_Java - Fatal编程技术网

如何处理以下代码中的运行时错误?我正在编写一个代码,用java查找数字的第一位

如何处理以下代码中的运行时错误?我正在编写一个代码,用java查找数字的第一位,java,Java,如果你只是想处理一个异常,你必须用try/catch块来包围它。如果要查找数字的第一位,请使用以下代码: 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

如果你只是想处理一个异常,你必须用try/catch块来包围它。如果要查找数字的第一位,请使用以下代码:

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);
    }
  }
}
import java.util.*;
公共班机{
公共静态void main(字符串参数[]){
试一试{
扫描仪sc=新的扫描仪(System.in);
System.out.println(“输入编号:”);
int number=sc.nextInt();
布尔状态=真;
当(州){

如果((数字>=10)|(请编号post并添加异常和堆栈跟踪。--作为旁白:您的程序将永远不会进入循环或无法正常运行。如果您希望在此方法本身中处理异常,则不要使用throws关键字。请使用try catch块包围您的方法体。在catch参数中,您可以给出异常e,然后是i在catch body中,可以打印异常。
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");
    }
}
}