【问题标题】:Errors in Java check integer is inputted输入 Java 检查整数中的错误
【发布时间】:2020-04-12 19:58:57
【问题描述】:

我知道有人问过这个问题,但我尝试应用我在此处看到的内容并收到错误。

package com.company;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner get_input = new Scanner(System.in);
        System.out.println("Enter your name ");
        String name = get_input.nextLine();


        boolean is_int = false;
        int year_of_birth = 0;
        System.out.println("Enter your year of birth");
        while (!get_input.hasNextInt()) {
        // If the input isn't an int, the loop is supposed to run
        // until an int is input.

            get_input.hasNextInt();
            year_of_birth = get_input.nextInt();
        }
        //year_of_birth = get_input.nextInt();

        System.out.println("Enter the current year");
        int current_year=get_input.nextInt();


        int age = current_year-year_of_birth;


        System.out.println("Your name is " + name + " and you are " + age + " year old.");

        get_input.close();

    }
}

没有循环,一切正常。我的代码有什么问题?为了清楚起见,我试图要求输入,直到输入可以被验证为整数。

非常感谢。

【问题讨论】:

  • 那个 wgile 循环没有意义
  • 正如 Antoniossss 所提到的,使用该循环您不会有任何收获。您正在做的是要求扫描仪将字符串解析为 int only 如果无法解析。尝试删除该感叹号或澄清您要执行的操作。
  • 在循环中,我想检查输入是否为整数,然后将其分配给int year_of_birth。我得到:“线程“主”java.util.InputMismatchException 中的异常”。我尝试使用布尔值作为中间值,然后将 hasNextInt 的结果分配给布尔值,但我无法做到。
  • 而你正在做 while(输入不是整数) => 分配整数。
  • 我正在尝试“当(输入不是整数)尝试请求新输入并测试这是否是整数”。不知道是不是很清楚。

标签: java loops input


【解决方案1】:

如果您想跳过无效的非整数值,您的循环应如下所示:

    while (!get_input.hasNextInt()) {
        // skip invalid input
        get_input.next();
    }
    // here scanner contains good int value  
    year_of_birth = get_input.nextInt();

【讨论】:

    【解决方案2】:

    如果我理解正确,这对我有用。您需要不断检查扫描仪有什么值,因此您需要在扫描仪中保持前进,而该值不是整数:

            Scanner get_input = new Scanner(System.in);
            System.out.println("Enter your name ");
            String name = get_input.nextLine();
    
            int year_of_birth = 0;
    
            System.out.println("Enter your year of birth");
            while (!get_input.hasNextInt()) { //check if it is not integer
                System.out.println("Enter your year of birth"); // ask again
                get_input.next(); //advance through the buffer
            }
            year_of_birth = get_input.nextInt(); //here you get an integer value
            int current_year=get_input.nextInt();
            int age = current_year-year_of_birth;
            System.out.println("Your name is " + name + " and you are " + age + " year old.");
    
            get_input.close();
    

    【讨论】:

    • 谢谢。你能解释一下为什么我的代码不起作用吗?
    • 在 while 循环内,您正在分配值,如果您想要做的是不断询问用户直到您获得一个整数值,在 while 循环内您需要跳过所有您设置的值不想要。因此,如果函数 hasNextInt 返回 false,我们不需要该值,因此我们使用 next() 跳过它。使用这个逻辑,一旦你完成循环,它就必须是因为它的值是一个整数。
    • 谢谢。为什么我收到错误“main”java.util.InputMismatchException”?在你的循环中,将值分配给 year_of_birth 的原因是什么?
    • 哎呀,我忘了最后一行来分配值 year_of_birth。我编辑了帖子。
    • 谢谢。不能在循环中打印两条消息吗?如果我尝试这样做,循环只会运行一次。
    猜你喜欢
    • 1970-01-01
    • 2014-03-28
    • 1970-01-01
    • 2012-09-08
    • 2011-11-04
    • 1970-01-01
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多