5

我今天刚开始一个新的java项目,我遇到了println的问题。这是我的主要方法:

public static void main(String[] args) {
    String stringNumGuards = JOptionPane.showInputDialog("How any guards do you have?");
    int numGuards = Integer.parseInt(stringNumGuards);
    Controller headGuard = new Controller();
    System.out.println("You have ", numGuards, " guards");
} //main

javac 输出

Controller.java:10: cannot find symbol
symbol  : method println(java.lang.String,int,java.lang.String)
location: class java.io.PrintStream
        System.out.println("You have ", numGuards, " guards");

我做错了什么?我以前从未遇到过 println 的问题。

4

3 回答 3

12

+你用not连接字符串,

System.out.println("You have ", numGuards, " guards");

应该成为

System.out.println("You have " + numGuards + " guards");
于 2013-07-30T22:51:55.043 回答
3

你需要有你的 println 像这样:

System.out.println("You have " + numGuards + " guards");

这将字符串与您放入 println 语句的变量连接起来。

于 2013-07-30T22:52:53.477 回答
0

在java中,你必须给出+符号而不是,方法println来连接字符串。所以你必须像这样输入。

System.out.println("You have " + numGuards + " gurads");
于 2017-10-23T08:10:47.113 回答