使用nextInt获取Java中的输入

使用nextInt获取Java中的输入,java,input,Java,Input,我正在编写一个Java程序,在这个程序中,用户输入三个科目的分数,并以此为基础获得他们的分数,但在获取输入时出错。代码如下: 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 g

我正在编写一个Java程序,在这个程序中,用户输入三个科目的分数,并以此为基础获得他们的分数,但在获取输入时出错。代码如下:

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);
    }
}

我做错了什么,怎么解决呢?

这对我来说很有效,我猜你输入的不是整数,不是文本,也可能是带小数点的数字

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

如果您需要处理非数字输入,例如,如果有人输入“1”而不是“1”,则需要使用.next()中的
将输入值作为字符串读取,然后将其转换为整数(如果需要十进制数,则转换为浮点数或双精度),如果不转换,则捕获异常。作为一个示例,您可以这样做:

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.
}
    boolean inputOk = false;
    do{
        System.out.println("enter your first grade");
        try {
            a = in .nextInt();
            inputOk = true;
        } catch (InputMismatchException  e) {
            System.out.println("not a valid number");
            in.nextLine(); // to reset the scanner
        }
    }while (!inputOk);

最后,检查你的分数条件,我认为其中至少有一个是错误的(提示:尝试你的代码,分数正好在年级边界上-例如40、45、60和80)。

这对我很有效,我猜你输入的不是整数-文本或可能是带小数点的数字

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

如果您需要处理非数字输入,例如,如果有人输入“1”而不是“1”,则需要使用.next()中的
将输入值作为字符串读取,然后将其转换为整数(如果需要十进制数,则转换为浮点数或双精度),如果不转换,则捕获异常。作为一个示例,您可以这样做:

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.
}
    boolean inputOk = false;
    do{
        System.out.println("enter your first grade");
        try {
            a = in .nextInt();
            inputOk = true;
        } catch (InputMismatchException  e) {
            System.out.println("not a valid number");
            in.nextLine(); // to reset the scanner
        }
    }while (!inputOk);
最后,检查你的分数条件,我认为其中至少有一个是不正确的(提示:尝试你的代码,分数正好在年级边界上-例如40、45、60和80)。

来自以下文档:

抛出:InputMismatchException-如果下一个标记与 整数正则表达式,或超出范围

因此,如果用户输入错误,您应该处理此异常。可能是这样的:

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.
}
    boolean inputOk = false;
    do{
        System.out.println("enter your first grade");
        try {
            a = in .nextInt();
            inputOk = true;
        } catch (InputMismatchException  e) {
            System.out.println("not a valid number");
            in.nextLine(); // to reset the scanner
        }
    }while (!inputOk);
对其他变量执行相同的操作。您甚至可以编写另一种方法来执行此操作,并避免重复您自己(干燥原则)

的文档:

抛出:InputMismatchException-如果下一个标记与 整数正则表达式,或超出范围

因此,如果用户输入错误,您应该处理此异常。可能是这样的:

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.
}
    boolean inputOk = false;
    do{
        System.out.println("enter your first grade");
        try {
            a = in .nextInt();
            inputOk = true;
        } catch (InputMismatchException  e) {
            System.out.println("not a valid number");
            in.nextLine(); // to reset the scanner
        }
    }while (!inputOk);

对其他变量执行相同的操作。您甚至可以编写另一种方法来执行此操作,并避免重复您自己的操作(枯燥的原则)

输入代码对我很有效,但您的if语句有缺陷。您需要更多的“其他”语句:

if (avg > 80) {
  gr = "distinction";
} else if (avg >= 60 && avg < 80) {
  gr = "first division";
} else if (avg >= 45 && avg < 60) {
  gr = "second division";
} else if (avg >= 40 && avg < 45) {
  gr = "pass";
} else {
  gr = "fail";
} 
if(平均值>80){
gr=“区别”;
}否则如果(平均值>=60&&avg<80){
gr=“第一师”;
}否则如果(平均值>=45&&avg<60){
gr=“第二分部”;
}否则如果(平均值>=40&&avg<45){
gr=“通过”;
}否则{
gr=“失败”;
} 

输入代码对我有效,但您的if语句有缺陷。您需要更多的“其他”语句:

if (avg > 80) {
  gr = "distinction";
} else if (avg >= 60 && avg < 80) {
  gr = "first division";
} else if (avg >= 45 && avg < 60) {
  gr = "second division";
} else if (avg >= 40 && avg < 45) {
  gr = "pass";
} else {
  gr = "fail";
} 
if(平均值>80){
gr=“区别”;
}否则如果(平均值>=60&&avg<80){
gr=“第一师”;
}否则如果(平均值>=45&&avg<60){
gr=“第二分部”;
}否则如果(平均值>=40&&avg<45){
gr=“通过”;
}否则{
gr=“失败”;
} 

您键入了什么?您键入了什么?这是不正确的;你发布的代码和原始代码是等价的。“你需要更多的‘else’语句?”-嗯?你怎么可能声称我的代码和原始代码是等价的?你试过运行它们吗?假设平均值等于83。最初的代码将gr设置为Differention,但最后一个if-else将设置为fail。我的代码没有。抱歉,你完全正确-我只是被你的断言“你需要更多‘其他’语句”弄糊涂了。也许编辑一下你在上面的评论中所说的,只是为了让它更清楚一点——Arnav的评论表明我不是唯一一个没有领会你观点的人。这是不正确的;你发布的代码和原始代码是等价的。“你需要更多的‘else’语句?”-嗯?你怎么可能声称我的代码和原始代码是等价的?你试过运行它们吗?假设平均值等于83。最初的代码将gr设置为Differention,但最后一个if-else将设置为fail。我的代码没有。抱歉,你完全正确-我只是被你的断言“你需要更多‘其他’语句”弄糊涂了。也许编辑一下你在上面的评论中所说的,只是为了让它更清楚一点——Arnav的评论表明我不是唯一一个没有领会你观点的人。