java.util.InputMismatchException扫描器问题?

huangapple go评论91阅读模式
英文:

java.util.InputMismatchException scanner issue?

问题

我正在尝试通过键盘扫描器收集用户输入,并验证输入是否与存储的值匹配。因此,如果输入是 2927942074l,应该显示行 Correct,但实际上我收到了以下错误。此外,如果输入与 PIN 不匹配,则会显示行 Wrong。不确定我做错了什么。

import java.util.Scanner;

class app {
  public static void main(String[] args)
  {
    long pin = 2927942074l;

    System.out.println("Please enter your pin.");
    Scanner keyboard = new Scanner(System.in);

    long input = keyboard.nextLong();

    if (input != pin)
    System.out.println("Wrong");
    if (input == pin)
    System.out.println("Correct");
  }
}
英文:

I am trying to collect user input via the keyboard scanner and verifying if the input matches the stored value. So if the input is 2927942074l the line Correct should appear however I get the following error. Also if the input doesn't match with the pin the line Wrong appears. Not sure what I am doing wrong.

Exception in thread "main" java.util.InputMismatchException
    at java.base/java.util.Scanner.throwFor(Scanner.java:939)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextLong(Scanner.java:2373)
    at java.base/java.util.Scanner.nextLong(Scanner.java:2328)
    at app.main(app.java:11)
import java.util.Scanner;

class app {
  public static void main(String[] args)
  {
    long pin = 2927942074l;

    System.out.println("Please enter your pin.");
    Scanner keyboard = new Scanner(System.in);

    long input = keyboard.nextLong();

    if (input != pin)
    System.out.println("Wrong");
    if (input == pin)
    System.out.println("Correct");
  }
}

答案1

得分: 0

你的代码是正确的,并且按照要求工作。

你需要输入的是 2927942074,而不是 2927942074l

英文:

Your code is correct and works as per requirement.

The thing that you need to input is 2927942074 not 2927942074l

答案2

得分: 0

Type just the number `2927942074` without trailing 'l'. Else it will treat it as a String.
Also you may want to add a condition using keyboard.hasNextLong() if you dont want to take String inputs.

    if (keyboard.hasNextLong()) {
        long input = keyboard.nextLong();
        if (input != pin)
            System.out.println("Wrong");
        if (input == pin)
            System.out.println("Correct");
    } else {
        System.out.println("Enter a valid pin");
    }
英文:

Type just the number 2927942074 without trailing 'l'. Else it will treat it as a String.
Also you may want to add a condition using keyboard.hasNextLong() if you dont want to take String inputs.

if (keyboard.hasNextLong()) {
		long input = keyboard.nextLong();
		if (input != pin)
			System.out.println("Wrong");
		if (input == pin)
			System.out.println("Correct");
} else {
		System.out.println("Enter a valid pin");
}

答案3

得分: 0

你必须输入

2927942074

而不是

2927942074l

“L” 只是为了让 Java 编译器理解这些数字表示的是长整型而不是整型(整型是默认的)。

英文:

You have to input

2927942074

not

2927942074l

The "L" is only for the Java Compiler to understand, that die digits resemble a long and not an int (which would be default).

huangapple
  • 本文由 发表于 2020年8月28日 14:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63628694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定