java - 如何测试字符串以查看它是否包含两个整数,如果包含,我需要在 acker 函数中使用它们

标签 java

这就是我到目前为止所拥有的,如果字符串有两个整数,我需要将它们取出并在接受两个整数作为参数的 acker 函数中使用它们。

public static void main(String []args)
{
    String nextLine = "";
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));

    AckerFunction x = new AckerFunction();

    System.out.println("Input two integers separated by a space character (enter q to quit):");
    try {
          nextLine = input.readLine(); 
    } 
    catch (IOException e) {
          e.printStackTrace();
    }

    if (nextLine.matches("[0-9]+") && nextLine.length() > 2) 
    {
        x.acker(m, n)
    }

最佳答案

我想您想从控制台获取两个整数。使用 nextInt() 方法,如下面所示的代码所示。

public static void main(String[] args) {
        AckerFunction x = new AckerFunction();      
        Scanner input = new Scanner(System.in);
        System.out.println("Input two integers separated by a space character");
        try {
            int m = input.nextInt();
            int n = input.nextInt();
            x.acker(m,n);
        }
        catch(InputMismatchException e) {
            System.out.println("Incorrect input, please enter integers");
        }
        finally {
            input.close();
        }  
    }

关于java - 如何测试字符串以查看它是否包含两个整数,如果包含,我需要在 acker 函数中使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28729221/

相关文章:

java - 面向 Java EE 开发人员的 Eclipse IDE

java - 与 T 不兼容的类型

java - 如何使用java单击网页上的超链接而不在浏览器中打开页面

java - 如何为VBox的 child 设置不同的高度

java - 在 Android 上将 SpongycaSTLe 与 iText 结合使用时出现 NoSuchMethodError

java - 什么是 Facelets?它与 JSF 有何关系?

java - 获取自己的日、年和月

java - Spark : configuration file 'metrics.properties'

java - 虚拟机上的 Selenium Grid - 如何配置

java - 为什么这个多线程程序会陷入死循环?