Reputation: 57
My problem arises in the if statement - in the event that the variable "hours" is greater than 10, i wish to multiply the additional hours by 2.00 and add 9.95 to the result but i receive an error at compilation and I don't understand why. Any help is much appreciated.
String choice;
double hours;
Scanner input = new Scanner(System.in);
System.out.print("Enter which package you are using A, B or C? ");
choice = input.nextLine();
choice = choice.toUpperCase();
System.out.print("Enter the amount of hours used: ");
hours = input.nextDouble();
switch ( choice )
{
case "A":
if ( hours > 10 ){
(( hours - 10) * 2 ) + 9.95; << ERROR: Not a statement!
}
else
System.out.println("Total: $9.95");
break;
For references sake this has been answered and edited to:
case "A":
if ( hours > 10 ){
total = (( hours - 10) * 2 ) + 9.95; // Initialised to total
System.out.println("Total: $" + total);
}
else
System.out.println("Total: $9.95");
break;
Upvotes: 0
Views: 2180
Reputation: 32478
That line is not a Java statement, As per Oracle doc, statement should be a complete unit of execution
Statements
Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon (;).
- Assignment expressions
- Any use of ++ or --
- Method invocations
- Object creation expressions
Upvotes: 1
Reputation: 4923
You are not setting this expression value (( hours - 10) * 2 ) + 9.95;
to any variable.
Set the value to any variable like below :
double total = (( hours - 10) * 2 ) + 9.95;
Upvotes: 1
Reputation: 2158
You have to assign your result to the hours variable like this:
hours = (your expression here);
Upvotes: 0
Reputation: 8657
You need to assign the result value that generated form that line like:
double value = (( hours - 10) * 2 ) + 9.95;
Read about valid statement in Java
Upvotes: 2