简体   繁体   English

为什么这会引发错误/根本不运行?

[英]Why does this throw an error/not run at all?

I'm running this in VS code and compiling and running through terminal with the proper JDK installed but nothing seems to run it just goes blank and then I type "clear" and a mismatch exception is thrown.我在 VS 代码中运行它,并在安装了正确 JDK 的情况下通过终端编译和运行,但似乎没有任何运行,它只是变成空白,然后我输入“clear”并引发不匹配异常。 Can someone explain why?有人可以解释为什么吗? Thank you:)谢谢:)

import java.util.Scanner;
class realTime {

    public static void Time(int seconds) {

        int min = 0, hours = 0;

        min = (int)seconds / 60;
        hours = (int)seconds / 3600;

        System.out.println("Hours: " + hours);
        System.out.println("Minutes: " + min);

    }

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        int seconds = scan.nextInt();

        Time(seconds);
    }
}

I know that it will not calculate the min and hours correctly I'm just putting code down to test whether it runs.我知道它不会正确计算分钟和小时我只是放下代码来测试它是否运行。

Because the terminal is waiting for some value, in this lines:因为终端正在等待某个值,所以在这行中:

Scanner scan = new Scanner(System.in);
int seconds = scan.nextInt();

Basically you're creating a Scanner object, and declaring an int variable waiting for a value, in this case a next integer, to do something.基本上,您正在创建一个扫描仪 object,并声明一个等待值的 int 变量,在本例中为下一个 integer,以执行某些操作。

Just type some integer number in your terminal and it should be working fine.只需在终端中输入一些 integer 号码,它应该可以正常工作。

In the terminal the scanner is waiting for the user input which is of type int, you need to enter the seconds in the terminal.在终端中,扫描仪正在等待 int 类型的用户输入,您需要在终端中输入秒数。

The error is showing because you are entering String type value but the scanner is expecting int type.显示错误是因为您正在输入 String 类型值,但扫描仪需要 int 类型。

Hope this resolve the issue.希望这能解决问题。

The solution is already present in other answers, but why the InputMismatchException is what this answer is about.该解决方案已经存在于其他答案中,但为什么InputMismatchException是这个答案的意义所在。

From the docs,从文档中,

For example, this code allows a user to read a number from System.in:例如,此代码允许用户从 System.in 中读取一个数字:

 Scanner sc = new Scanner(System.in); int i = sc.nextInt();

For nextInt()对于nextInt()

public int nextInt()公共 int nextInt()

Scans the next token of the input as an int.将输入的下一个标记扫描为 int。

Throws: InputMismatchException - if the next token does not match the Integer regular expression, or is out of range抛出: InputMismatchException - 如果下一个标记与 Integer 正则表达式不匹配,或者超出范围

Here "clear" does not match the regular expression of Integer.这里的“clear”不匹配Integer的正则表达式。

Since you are not passing any radix to nextInt() method, it expects the input to have only 0-9 (Decimal Number System- Base 10 is the default).由于您没有将任何基数传递给nextInt()方法,因此它希望输入只有 0-9(十进制数系统 - 默认为 10 进制)。 Had it been nextInt(16) , It would accept any HexaDecimal (0-F, Base 16) Number and so on.如果它是nextInt(16) ,它将接受任何 HexaDecimal (0-F, Base 16) Number 等等。

It is worth noting the another case where you can get the same Exception, This happens if the input > Integer.MAX_VALUE .值得注意的另一种情况是您可以获得相同的异常,如果input > Integer.MAX_VALUE会发生这种情况。

You need to have a prompt for the user to enter in there time in seconds so your scanner can actually get something to put in for your seconds int.您需要提示用户以秒为单位输入时间,以便您的扫描仪实际上可以为您的秒数输入一些内容。 Do something like this:做这样的事情:

System.out.println("Enter seconds to convert");
   int seconds = scan.nextInt();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM