Reputation: 11
import java.util.*; // needed for Scanner class
public class PhoneCalls
{
public static void main (String[] args)
{
Scanner reader = new Scanner(System.in); // needed to read user input
System.out.println ("Enter the length of a phone call in minutues (an integer). ");
int length=reader.nextInt();
System.out.println ("Do you have a discount plan (answer 1 for yes and 2 for no):");
int plan=reader.nextInt();
(double) cost;
if (length<=3 && plan==2)
(double) cost=.2*length;
else if (length<=3 && plan==1)
(double) cost= 0.2*length*0.6;
else if (length>3 && plan==2)
(double) cost = (.2*3)+(.08*(length-3));
else (double) cost = .2*3 + (.08*(length-3)* 0.6);
System.out.println("The cost of your call is $ ");
System.out.printf("%.2f.", cost);
}
}
This is to find length of a phone call (an integer) in minutes, and print the cost. Error: '(double) cost;' is not a statement. How do I fix this error?
Upvotes: 0
Views: 346
Reputation: 13596
Replace:
(double) cost;
With
double cost;
And get rid of all the (double)
s in your code.
(double)
is used to cast another data type to a double.
Upvotes: 1
Reputation: 178303
A type name in parentheses such as (double)
means to cast another value as that datatype. To declare a variable with that datatype, remove the parentheses.
double cost;
When referring to a variable that's already declared, there is no need to specify the type again. Remove (double)
in multiple locations, such as:
if (length<=3 && plan==2)
{
cost=.2*length;
}
Upvotes: 1