You need to use a ternary operator ?:
instead of if / else
... " feet through air is " + (getSpeedInAir() > 0 ? : formatter.format(getSpeedInAir()) ...
문제
So I am working on an assignment where I have to have a print method in a constructor that displays a distance. I also have to have three separate get methods depending on what the input is in the demo class. My question is that I am trying to write the print method to contain a decision structure based on which get is used. 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";
)
}
After trying to compile I am getting the following errors.
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
해결책
You need to use a ternary operator ?:
instead of if / else
... " feet through air is " + (getSpeedInAir() > 0 ? : formatter.format(getSpeedInAir()) ...
다른 팁
errror in this line
"error";)
check this line you might write something like this
System.out.println("error");
There are probably many errors in your program.
1) You dont have an if
statement. That is why you have got the error that else without if
.
2) You last else is wrong:
else
"error";)
You may proably try like this:
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");
or more efficiently like this:-
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);
You can't use an if-statement inside the System.out.println
like that.
You could do something with the ternary operator, but for the sake of code readability, it's probably best to just declare a variable and concatenate like so:
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);