如何修复'不是声明'?

时间:2014-04-22 21:43:58

标签: java compiler-errors variable-assignment

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);

  } 
}  

这是以分钟为单位查找电话呼叫长度(整数),并打印费用。 错误:'(双倍)成本;'不是声明。如何解决此错误?

2 个答案:

答案 0 :(得分:1)

替换:

(double) cost;

使用

double cost;

删除代码中的所有(double)

(double)用于将另一种数据类型转换为double。

答案 1 :(得分:1)

括号中的类型名称(例如(double))表示将另一个值转换为该数据类型。要声明具有该数据类型的变量,请删除括号。

double cost;

当引用已经声明的变量时,不需要再次指定类型。在多个位置删除(double),例如:

if (length<=3 && plan==2)    
{     
   cost=.2*length; 
}
相关问题