所以我正在进行一项任务,我必须在显示距离的构造函数中使用print方法。 我还必须有三个单独的get方法,具体取决于demo类中的输入。 我的问题是我正在尝试编写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
答案 0 :(得分:0)
您的计划可能存在很多错误。
1)你没有if
陈述。这就是为什么你得到else without if
。
2)你最后别的错了:
else
"error";)
你可以这样尝试:
if(getSpeedInAir() > 0)
{
System.out.println("The time it takes the sound to travel " + distance +
" feet through air is " +formatter.format(getSpeedInAir());
}
else if (getSpeedInWater() > 0)
{
System.out.println("The time it takes the sound to travel " + distance +
" feet through air is " + formatter.format(getSpeedInWater());
}
else if (getSpeedInSteel() > 0)
{
System.out.println("The time it takes the sound to travel " + distance +
" feet through air is " + formatter.format(getSpeedInSteel());
}
else
System.out.println("The time it takes the sound to travel " + distance +
" feet through air is "+ "error");
或更有效地这样: -
String s;
if (getSpeedInAir() > 0)
{
s= formatter.format(getSpeedInAir()).toString();
}
else if (getSpeedInWater() > 0)
{
s= formatter.format(getSpeedInWater()).toString();
}
else if (getSpeedInSteel() > 0)
{
s= formatter.format(getSpeedInSteel()).toString();
}
else
s= "error";
System.out.println("The time it takes the sound to travel " + distance +
" feet through air is " + s);
答案 1 :(得分:0)
此行中的错误
"error";)
检查此行 你可能会写这样的东西
System.out.println("error");
答案 2 :(得分:0)
您需要使用三元运算符?:
而不是if / else
... " feet through air is " + (getSpeedInAir() > 0 ? : formatter.format(getSpeedInAir()) ...
答案 3 :(得分:0)
您不能在System.out.println
内使用if语句。
你可以对ternary operator做一些事情,但为了代码可读性,最好只是声明一个变量并连接如下:
String speed;
if (getSpeedInAir() > 0)
{
speed = formatter.format(getSpeedInAir()).toString();
}
else if (getSpeedInWater() > 0)
{
speed = formatter.format(getSpeedInWater()).toString();
}
else if (getSpeedInSteel() > 0)
{
speed = formatter.format(getSpeedInSteel()).toString();
}
else
speed = "error";
System.out.println("The time it takes the sound to travel " + distance +
" feet through air is " + speed);