首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我在TCS考试中遇到了inputMissMatch异常

我在TCS考试中遇到了inputMissMatch异常
EN

Stack Overflow用户
提问于 2022-02-23 10:05:48
回答 1查看 18关注 0票数 0
代码语言:javascript
复制
public class Solution {

    public static void main(String args[])
        Scanner sc=new Scanner(System.in);
        for(int i=0;i<5;i++){
            int input1=sc.nextInt();
            sc.nextLine();
            String str1=sc.nextLine();
            Double input2=sc.nextDouble();
        }
    }

}

这是我的密码。我在"int“和"double”输入中只在for循环中获得了此异常。如果我把它写在for循环的外面,它就能正常工作。请任何人都能帮我解决这个问题,因为我在TCS考试中遇到了这个错误。

EN

回答 1

Stack Overflow用户

发布于 2022-02-23 10:32:52

不幸的是,你的问题并没有给我多少信息,但我会尝试回答无论如何(需要那个代表)。我假设你遇到的问题是一个InputMismatchException?这可能与您试图将double (即12.0)传递到int有关。使用类似System.out.println("Enter Int Input:")的内容可以帮助您突出显示应该在行中提供的输入类型。try/catch块可以停止抛出错误。

代码语言:javascript
复制
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);

    try {
        for (int i = 0; i < 5; i++) {

            System.out.println("i: " + i);
            System.out.println("Enter Int Value:");
            int input1 = sc.nextInt();
            sc.nextLine();
            System.out.println("Enter String Value:");
            String str1 = sc.nextLine();
            System.out.println("Enter Double Value:");
            Double input2 = sc.nextDouble();
        }
    } catch (InputMismatchException e) {
        System.out.println("Invalid input entered for variable type.");

    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71234910

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档