所以我正在做一个分配,我必须在一个显示距离的构造函数中有一个打印方法。根据演示类中的输入内容,我还必须拥有三个单独的 get 方法。我的问题是我正在尝试编写 print 方法以包含基于使用哪个 get 的决策结构。public void prt() { DecimalFormat formatter = new DecimalFormat("#,##0.00");
System.out.println(
"The time it takes the sound to travel " + distance +
" feet through air is " +
if (getSpeedInAir() > 0) {
formatter.format(getSpeedInAir());
}
else if (getSpeedInWater() > 0) {
formatter.format(getSpeedInWater());
}
else if (getSpeedInSteel() > 0) {
formatter.format(getSpeedInSteel());
}
else "error";
)
}
尝试编译后出现以下错误。
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\GlaDOS\Desktop\JavaStuff>j Speed
C:\Users\GlaDOS\Desktop\JavaStuff>del *.class
C:\Users\GlaDOS\Desktop\JavaStuff>javac Speed.java
Speed.java:43: error: illegal start of expression
" feet through air is
" + if (getSpeedInAir() > 0)
^
Speed.java:43: error: ';' expected
" feet through air is
" + if (getSpeedInAir() > 0)
^
Speed.java:43: error: not a statement
" feet through air is
" + if (getSpeedInAir() > 0)
^
Speed.java:43: error: ';' expected
" feet through air is
" + if (getSpeedInAir() > 0)
^
Speed.java:47: error: 'else' without 'if'
else if (getSpeedInWater() > 0)
^
Speed.java:56: error: not a statement
"error";)
^
Speed.java:56: error: illegal start of expression
"error";)
^
7 errors
C:\Users\GlaDOS\Desktop\JavaStuff>java Speed
Error: Could not find or load main class Speed