线程“main”中的异常java.util.regex.PatternSyntaxException:未闭合的字符类

时间:2017-09-05 21:49:29

标签: java

我在java代码中收到一条奇怪的错误消息... 我正在尝试制作一个可以将两个数字组合在一起的简单计算器,但它不起作用,因为我是Java的新手......这是错误代码:

package testing;
import java.util.Scanner; 
public class Test1 {
    public static Scanner scan;
    public static void main(String[] args) {
    scan = new Scanner(System.in);
    int n1 = 0;
    System.out.print("What would you like to add");
    System.out.print(scan.nextInt(n1) + "You have selected" + scan.nextInt(n1));
    int n2 = 0;
    System.out.print(scan.nextInt(n2) + "You have selected" + scan.nextInt(n2));
    System.out.print("Calculation=" + n1+n2);
        }
    }

我的代码是

Jenkins

我无法弄清楚为什么它不起作用而且它正在我的头脑中 - .- 我是Java的新手,所以如果你能帮助我,我会非常感激:)

2 个答案:

答案 0 :(得分:1)

我认为你应该尝试不同的方法,这样的东西更具可读性,而且在我看来,如果你开始的话,更容易使用。

public class Main {


public static void main(String[] args) {
    Scanner scan ;
    scan = new Scanner(System.in);
    System.out.println("What would you like to add");
    int n1 = scan.nextInt();
    System.out.println( "You have selected" + n1);
    int n2 = scan.nextInt();
    System.out.println( "You have selected" + n2);
    int calculation= n1+n2;
    System.out.println("Calculation=" + calculation);
}

答案 1 :(得分:0)

我相信这就是你要做的事情:

public class Testing {

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

        System.out.println("What would you like to add");
        int n1 = scan.nextInt();
        System.out.println("You have selected" + n1);

        int n2 = scan.nextInt();
        System.out.println("You have selected" + n2);

        System.out.println("Calculation=" + (n1 + n2));

        scan.close();
    }
}

有几点需要注意:

  1. 需要将nextInt()分配给变量(不传递给它)

  2. n1 + n2需要放在paranthesis(n1 + n2)中,否则将被处理为String。