Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何提示用户输入有效的整数?_Java_Integer_Java.util.scanner - Fatal编程技术网

Java 如何提示用户输入有效的整数?

Java 如何提示用户输入有效的整数?,java,integer,java.util.scanner,Java,Integer,Java.util.scanner,作为字母的有效整数。我不知道让它检查字符串的命令,也不知道在哪里可以找到它。任何帮助都将不胜感激 import java.util.Scanner; public class Stringtest{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int test = 10; while (test>0){ System.out.prin

作为字母的有效整数。我不知道让它检查字符串的命令,也不知道在哪里可以找到它。任何帮助都将不胜感激

import java.util.Scanner;

public class Stringtest{

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    int test = 10;

    while (test>0){
    System.out.println("Input the maximum temperature.");
    String maxTemp = input.nextLine();
    System.out.println("Input the minimum temperature.");
    String minTemp = input.nextLine();
    }
}
}
您应该使用Integer.parseInt,即用户可以输入任何字符串,然后您可以使用此api检查它是否为整数,如果它不是整数,您将获得一个异常,然后您可以从用户处获取另一个条目。查看下面的链接了解用法

只需使用input.nextInt,然后对无效的int值执行简单的try-catch

您也不应该尝试将温度索引保存为字符串。

使用获取下一个整数值。 如果用户键入非整数值,您应该尝试/捕获它

下面是一个例子:

Scanner input = new Scanner(System.in);
// infinite loop
while (true){
        System.out.println("Input the maximum temperature.");
        try {
            int maxTemp = input.nextInt();
            // TODO whatever you need to do with max temp
        }
        catch (Throwable t) {
            // TODO handle better
            t.printStackTrace();
            break;
        }
        System.out.println("Input the minimum temperature.");
        try {
            int minTemp = input.nextInt();
            // TODO whatever you need to do with min temp
        }
        catch (Throwable t) {
            // TODO handle better
            t.printStackTrace();
            break;
        }
}

也许你可以这样做:

Scanner scanner = new Scanner(System.in); 
int number;
do {
    System.out.println("Please enter a positive number: ");
    while (!scanner.hasNextInt()) {
        String input = scanner.next();
        System.out.printf("\"%s\" is not a valid number.\n", input);
    }
    number = scanner.nextInt();
} while (number < 0);

从这里开始:,并使用。只需使用input.nextInt,然后使用一个简单的try catch来获取无效的int。您也不应该尝试将温度索引保存为字符串。input.nextInt只接受整数?哇!我真是个傻瓜,谢谢。最好使用ScannerhasNextInt和ScannernextInt。Mena,查看OP的最新问题。看起来你不是在教他学习Java,而是在教他乞求代码;尽管如此,还是有一些工具来规范/调节社区。。。
Scanner scanner = new Scanner(System.in); 
int number;
do {
    System.out.println("Please enter a positive number: ");
    while (!scanner.hasNextInt()) {
        String input = scanner.next();
        System.out.printf("\"%s\" is not a valid number.\n", input);
    }
    number = scanner.nextInt();
} while (number < 0);