Java编译器错误 - 作业分配

时间:2011-04-21 02:15:57

标签: java compiler-construction instance

编译器给出了错误“'void'类型,这里不允许...< =运算符不能应用于java.lang.String,int ... not a statement”

getHours()和getSeconds()返回int类型的实例变量。任何帮助将不胜感激。

 if (userCommand.equals("a")) {
            yourClock.advance();
            System.out.println(yourClock.getSeconds());
            System.out.println("The time is now" +
                    (yourClock.getHours()) <= 9 ? ".0" : ".") +
                    yourClock.getHours() +
                    (yourClock.getMinutes() <= 9 ? ".0" : ".") +
                    yourClock.getMinutes() +
                    (yourClock.getSeconds() <= 9 ? ".0" : ".") +
                    yourClock.getSeconds();

3 个答案:

答案 0 :(得分:4)

您正在关闭println错误的地方。你在第一次getHours()调用之后关闭它

if (userCommand.equals("a")) {
            yourClock.advance();
            System.out.println(yourClock.getSeconds());
            System.out.println("The time is now" +
                    (yourClock.getHours() <= 9 ? ".0" : ".") +
                    yourClock.getHours() +
                    (yourClock.getMinutes() <= 9 ? ".0" : ".") +
                    yourClock.getMinutes() +
                    (yourClock.getSeconds() <= 9 ? ".0" : ".") +
                    yourClock.getSeconds());

答案 1 :(得分:2)

看起来你太早关闭了打印输出

(yourClock.getHours()) <= 9 ? ".0" : ".") +

之后

结束

getHours())

正在关闭您的打印输出。

答案 2 :(得分:1)

让我们看一下这句话:

(yourClock.getHours()) <= 9 ? ".0" : ".")

难道你不认为你缺少'('这里?

这样更好:

((yourClock.getHours() <= 9) ? ".0" : ".")