简体   繁体   English

如何修复“不是声明”?

[英]How to fix 'not a statement'?

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? 我该如何解决这个错误?

Replace: 更换:

(double) cost;

With

double cost;

And get rid of all the (double) s in your code. 并删除代码中的所有(double) s。

(double) is used to cast another data type to a double. (double)用于将另一种数据类型转换为double。

A type name in parentheses such as (double) means to cast another value as that datatype. 括号中的类型名称(如(double)表示将另一个值转换为该数据类型。 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: 在多个位置删除(double) ,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM