使用nextInt在Java中获取输入 [英] Getting input in Java using nextInt

查看:39
本文介绍了使用nextInt在Java中获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Java程序,其中用户输入三个主题的分数并根据该分数获得其分数,但是在获取输入时会出错.这是代码:

  import java.util.*;公共课程编{公共静态void main(String [] args){Scanner in =新的Scanner(System.in);int a,b,c;平均字符串gr;System.out.println(物理,化学,生物学标记");.nextInt()中的a =b =在.nextInt();中c =在.nextInt();中平均=(a + b + c)/3;如果(平均> 80){gr =区别";}if(avg> = 60&& avg< 80){gr =第一师";}if(avg> = 45&& avg< 60){gr =第二级";}if(avg> = 40&& avg< 45){gr =通过";} 别的 {gr =失败";}System.out.println(gr);}} 

它显示此错误:

 线程主"中的异常java.util.InputMismatchException在java.util.Scanner.throwFor(未知来源)在java.util.Scanner.next(未知源)在java.util.Scanner.nextInt(未知来源)在java.util.Scanner.nextInt(未知来源)在Prog.main(Prog.java:9) 

我在做什么错,我该如何解决?

解决方案

这对我有用,我猜您输入的输入不是整数-文本还是带小数点的数字?

如果是您需要能够处理带小数点的数字,我建议将变量设置为 a b c double float 类型,然后使用 in.nextDouble() in.nextFloat().

如果您需要处理非数字输入,例如,如果有人输入"1"而不是"1",那么您需要使用 in.next()将输入值读取为字符串.,然后将它们转换为整数(如果需要十进制数字,则将其转换为浮点数或双精度数),如果不转换则捕获异常.作为如何执行以下操作的示例:

  String inValue = in.next();尝试 {一个= Integer.parseInt(inValue);} catch(NumberFormatException ex){//做其他事情,例如退出并显示错误消息或将该值设置为0.} 

最后,请检查您的分数条件,我认为其中至少有一个是不正确的(提示:尝试在分数边界上正确设置分数的代码-例如40、45、60和80).

I am coding a Java Program in which user inputs marks of three subject and gets their grades based on that but getting an error in getting input. Here is the code:

import java.util.*;
public class Prog {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a, b, c;
        double avg;
        String gr;
        System.out.println("marks in Physics,Chemistry,Biology");
        a = in .nextInt();
        b = in .nextInt();
        c = in .nextInt();
        avg = (a + b + c) / 3;
        if (avg > 80) {
            gr = "distinction";
        }
        if (avg >= 60 && avg < 80) {
            gr = "first division";
        }
        if (avg >= 45 && avg < 60) {
            gr = "second division";
        }
        if (avg >= 40 && avg < 45) {
            gr = "pass";
        } else {
            gr = "fail";
        }
        System.out.println(gr);
    }
}

It shows this error :

Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at Prog.main(Prog.java:9)

What am I doing wrong, How can I solve it?

解决方案

This works for me, my guess is that the input you're typing is not an integer - either text or maybe a number with a decimal point?

If it's that you need to be able to handle numbers with decimal points, I'd suggest making the variables a, b and c either double or float types, and then using in.nextDouble() or in.nextFloat().

If you need to handle non-numeric input, for example, if someone enters "one" instead of "1", then you'll need to read the input values as Strings, with in.next(), and then convert them to integers (or floats or doubles if you need decimal numbers), and catch the exception if it doesn't convert. As an example of how you might do something like this:

String inValue = in.next();
try {
    a = Integer.parseInt(inValue);
} catch (NumberFormatException ex) {
    // Do something else, like exiting with an error message or setting the value to 0.
}

Lastly, check your conditions for the scores, I think at least one of them is incorrect (hint: try your code with scores right on the grade boundaries - e.g. 40, 45, 60 and 80).

这篇关于使用nextInt在Java中获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆