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);
}
} 这是为了找出电话通话的时长(一个整数),并打印出费用。错误:'(double) cost;‘不是语句。如何修复此错误?
发布于 2014-04-23 05:46:06
替换:
(double) cost;使用
double cost;并去掉代码中所有的(double)。
(double)用于将另一个数据类型转换为双精度型。
发布于 2014-04-23 05:47:41
括号中的类型名称(如(double) )表示将另一个值转换为该数据类型。要声明具有该数据类型的变量,请删除圆括号。
double cost;当引用已经声明的变量时,不需要再次指定类型。删除多个位置的(double),例如:
if (length<=3 && plan==2)
{
cost=.2*length;
}https://stackoverflow.com/questions/23230775
复制相似问题