gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 22:26:17 24 4
gpt4 key购买 nike

这就是我到目前为止所拥有的,如果字符串有两个整数,我需要将它们取出并在接受两个整数作为参数的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com