Java 测试输入是否为正整数

Java 测试输入是否为正整数,java,string,int,java.util.scanner,Java,String,Int,Java.util.scanner,好吧,我似乎找不到我问题的确切答案 我需要一个java程序,它可以返回用户输入的值,即正整数 为了将其与许多其他类似的问题区分开来,我需要编写程序,使其在输入字符串时不会崩溃。程序需要反复提示用户输入,直到输入正整数。脚本如下所示: Input: 7.2 Error, try again: -5 Error, try again: -2.4 Error, try again: 3.77777 Error, try again: -1 Error, try again: 0 Error, try

好吧,我似乎找不到我问题的确切答案


我需要一个java程序,它可以返回用户输入的值,即正整数


为了将其与许多其他类似的问题区分开来,我需要编写程序,使其在输入字符串时不会崩溃。程序需要反复提示用户输入,直到输入正整数。脚本如下所示:

Input: 7.2
Error, try again: -5
Error, try again: -2.4
Error, try again: 3.77777
Error, try again: -1
Error, try again: 0
Error, try again: -33
Error, try again: microwave
Error, try again: -1
Error, try again: 4.2
Error, try again: hello
Error, try again: 3.14159
Error, try again: 4
4 is a positive integer.
输入单词之类的内容时,程序不会崩溃,这一点很重要。我想不出一个方法让程序做这样的事情。我可以在扫描仪上使用
hasNextInt
,但是我不知道如何检查它是否大于零,因为在另一种情况下,输入可能是字符串

有人能帮我一个程序,将返回上述脚本吗?非常感谢

编辑:好吧,我终于在另一个问题中找到了我想要的答案。此解决方案不使用
parseInt()
,也不使用
try
/
catch
,这正是我要找的。又好又简单。下面是解决方案代码:

    Scanner in = new Scanner(System.in);
    int num;
    System.out.print("Input: ");
    do {
        while (!in.hasNextInt()) {
            System.out.print("Error, not integer. Try again: ");
            in.next(); 
        }
        num = in.nextInt();
        if (num<=0){
            System.out.print("Error, not positive. Try again: ");
        }
    } while (num <= 0);
    System.out.println(num + " is a positive integer.");
Scanner-in=新的扫描仪(System.in);
int-num;
系统输出打印(“输入:”);
做{
而(!in.hasNextInt()){
System.out.print(“错误,不是整数。请重试:”;
in.next();
}
num=in.nextInt();
如果(num

就是为了这个而写的。

试着这样做:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int value = -1;
    do {
        System.out.print("Input: ");
        String input = scanner.next();
        try {
            value = Integer.parseInt(input);
        } catch (NumberFormatException e) {
            System.out.println("Error, try again: " + input);
        }
    } while (value < 1);
    System.out.println(value + " is a positive integer.");
}
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
int值=-1;
做{
系统输出打印(“输入:”);
字符串输入=scanner.next();
试一试{
value=Integer.parseInt(输入);
}捕获(数字格式){
System.out.println(“错误,重试:“+input”);
}
}而(值<1);

System.out.println(值+“是一个正整数。”); }
这里是java中异常处理的一个示例。通常,您只需将代码放入一个try块。然后,如果该块中出现预期的异常,您的程序将在catch块中执行代码并继续工作。

您需要以某种方式验证输入。如果输入是一个
整数,则需要将其打印到屏幕上,如果输入的
较长
,则需要打印错误消息
重试

在下面的示例中,我创建了一个方法,它是
C#
TryParse
方法的派生

它的作用是将输入解析为一个
整数
,如果由于输入是
字符串
或不是有效的正
整数
,则抛出一个
数字格式异常
。然后我可以打印所需的错误消息


祝你好运!下面是两个场景:

情景1:
-我假设正整数是指整数1、2、3、4等。
-诸如4.00之类的值将不会被视为整数

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int value = 0;
        System.out.print("Input: ");

        while (value < 1){
            String input = scanner.next();
            try {
                value = Integer.parseInt(input);
                if(value < 1){
                    System.out.print("Error, try again: ");}
            } catch (NumberFormatException e) {
                System.out.print("Error, try again: ");
            }
        }
        System.out.println(value + " is a positive integer.");
    }
}
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Input: ");
        String value = "";
        boolean positiveIntegerNoDecimals = false;
        while (positiveIntegerNoDecimals == false) {
            value = scanner.next();
            positiveIntegerNoDecimals = isPositiveIntegerNoDecimals(value);

            if (positiveIntegerNoDecimals == false)
                System.out.print("Error, try again:");
        }
        System.out.println(value + " is a positive integer.");
    }

    private static boolean isPositiveIntegerNoDecimals(String input) {
        try {
            int value = -1;
            BigDecimal decimal = new BigDecimal(Double.parseDouble(input));
            int scale = decimal.scale();
            if (scale == 0)
                value = decimal.stripTrailingZeros().intValue();

            if (value > 0)
                return true;
        } catch (NumberFormatException e) {}
        return false;
    }
}

读入
nextLine
,使用
try catch
并解析为
Integer
,捕获异常。如果未发生异常,请检查
num@KevinEsche不太熟悉
try catch
,您能详细说明一下它在代码中的用法吗?对不起,我不是很聪明:(我不熟悉这种方法,你能告诉我它是如何在程序中使用的吗?给我们看一些你在哪里使用过scanner的代码?我会准确地指导你在哪里以及如何使用它。“正整数”我需要学习更多java:(但谢谢你,这是可行的!现在它是可执行的,接近你的要求。是的,学习Java!这个链接非常有用,谢谢你!
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.print("Input: ");
        String value = "";
        boolean positiveIntegerNoDecimals = false;
        while (positiveIntegerNoDecimals == false) {
            value = scanner.next();
            positiveIntegerNoDecimals = isPositiveIntegerNoDecimals(value);

            if (positiveIntegerNoDecimals == false)
                System.out.print("Error, try again:");
        }
        System.out.println(value + " is a positive integer.");
    }

    private static boolean isPositiveIntegerNoDecimals(String input) {
        try {
            int value = -1;
            BigDecimal decimal = new BigDecimal(Double.parseDouble(input));
            int scale = decimal.scale();
            if (scale == 0)
                value = decimal.stripTrailingZeros().intValue();

            if (value > 0)
                return true;
        } catch (NumberFormatException e) {}
        return false;
    }
}